home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / 3DDEMO.ZIP / 3D / SOURCE / POLYGFX.CPP < prev    next >
C/C++ Source or Header  |  1996-07-21  |  201KB  |  6,649 lines

  1. #include "polygfx.hpp"
  2.  
  3. // Copyright (c) 1996 by Kerrigan Burgess, all rights reserved.
  4.  
  5. int FindCode(int x, int y)
  6. {
  7.   int code;
  8.   
  9.   if (x<_MinClipX)
  10.     code=LEFT;
  11.   else
  12.   if (x>=_MaxClipX)
  13.     code=RIGHT;
  14.   else
  15.     code=0;
  16.  
  17.   if (y>=_MaxClipY)
  18.     code|=BOTTOM;
  19.   else
  20.   if (y<_MinClipY)
  21.     code|=TOP;
  22.  
  23.   return code;
  24. }
  25.  
  26.      // Clipping code gotton from book by Marc B. Sugiyama and Christopher D. Metcalf.
  27. void BresenHamLine(int x1,int y1,int x2,int y2,unsigned char color,
  28.                    unsigned char *Buffer)
  29. {                  
  30.   int code,code1,code2,x,y,dx,dy,x_inc,y_inc,error=0,count;
  31.  
  32.   code1=FindCode(x1,y1);
  33.   code2=FindCode(x2,y2);
  34.   
  35.   while (code1 | code2)            // while not fully clipped do.
  36.   {
  37.      if (code1 & code2)            // trivial rejection.
  38.        return;
  39.  
  40.      if (code1)
  41.        code=code1;
  42.      else
  43.        code=code2;         
  44.      
  45.      if (code & LEFT)
  46.      {
  47.         x=_MinClipX;
  48.         y=y1+((y2-y1)/(float)(x2-x1))*(_MinClipX-x1);
  49.      }
  50.      else
  51.      if (code & RIGHT)
  52.      {
  53.         x=_MaxClipX-1;
  54.         y=y1+((y2-y1)/(float)(x2-x1))*(x-x1);
  55.      }
  56.      else
  57.      if (code & BOTTOM)
  58.      {
  59.         y=_MaxClipY-1;
  60.         x=x1+((x2-x1)/(float)(y2-y1))*(y-y1);
  61.      }
  62.      else
  63.      if (code & TOP)
  64.      {
  65.         y=_MinClipY;
  66.         x=x1+((x2-x1)/(float)(y2-y1))*(_MinClipY-y1);
  67.      }
  68.      
  69.      if (code==code1)
  70.        code1=FindCode(x1=x,y1=y);
  71.      else
  72.        code2=FindCode(x2=x,y2=y);
  73.   }
  74.   
  75.   Buffer+=(y1<<8)+(y1<<6) + x1;
  76.  
  77.   dx=x2-x1;
  78.   dy=y2-y1;
  79.  
  80.   if (dx>=0)
  81.     x_inc=1;
  82.   else
  83.   {
  84.     x_inc=-1;
  85.     dx=-dx;
  86.   }
  87.  
  88.   if (dy>=0)
  89.     y_inc=SCREENWIDTH;
  90.   else
  91.   {
  92.     y_inc=-SCREENWIDTH;
  93.     dy=-dy;
  94.   }
  95.  
  96.   if (dx>dy)
  97.   {
  98.     for (count=0;count<=dx;count++)
  99.     {
  100.       *Buffer=color;
  101.       error+=dy;
  102.  
  103.       if (error>dx)
  104.       {
  105.         error-=dx;
  106.         Buffer+=y_inc;
  107.       }
  108.       Buffer+=x_inc;
  109.     }
  110.   }
  111.   else
  112.   {
  113.     for (count=0;count<=dy;count++)
  114.     {
  115.       *Buffer=color;
  116.       error+=dx;
  117.  
  118.       if (error>0)
  119.       {
  120.         error-=dy;
  121.         Buffer+=x_inc;
  122.       }
  123.       Buffer+=y_inc;
  124.     }
  125.   }
  126. }
  127.        
  128. void Scan_Convert_Wire(void)
  129. {
  130.   unsigned char color;
  131.  
  132.   color=LookPal[ _ColorIndex ];
  133.    
  134.   BresenHamLine( _X1,_Y1,_X2,_Y2,color,_RendBuffer);
  135.   BresenHamLine( _X2,_Y2,_X3,_Y3,color,_RendBuffer);
  136.   BresenHamLine( _X3,_Y3,_X1,_Y1,color,_RendBuffer);
  137. }
  138.  
  139. //void Scan_Convert_Lambert(void);    In assembly module polylow.asm
  140. //void Scan_Convert_Gouraud(void); In assembly module polylow.asm
  141.  
  142. void Scan_Convert_Phong(void)
  143. {
  144.   long LeftX, RightX, LeftDx, RightDx;
  145.   long LeftA,RightA,MiddleA;
  146.   long LeftDa,RightDa,MiddleDa;
  147.   long slope,height;
  148.   int x,y,newx,tempx,tempy,tempa,old_X3,old_Y3,old_A3;
  149.   int ydiff,newa,ClipLeftX,ClipRightX;
  150.   int GENERAL;
  151.   unsigned char *Buffer;
  152.  
  153.     if ( (_X1==_X2 && _X2==_X3) || (_Y1==_Y2 && _Y2==_Y3) )
  154.       return;               // degenerated into lines.       
  155.  
  156.     // NOTE: _A1,_A2,_A3 don't hold intensities but instead hold fixed point angles
  157.     // between light and avgnormal vector. We interpolate the angles and then
  158.     // index into a PhongTBL to get intensity at that angle. This amazing
  159.     // algorithm was thought up by Zach Mortensen (Voltaire/OTM.) mortens1@nersc.gov.
  160.  
  161.     if (_Y2<_Y1)              // switch. They're in the wrong order.
  162.     {
  163.         tempx=_X1; tempy=_Y1; tempa=_A1;
  164.         _X1=_X2;    _Y1=_Y2;    _A1=_A2;
  165.         _X2=tempx; _Y2=tempy; _A2=tempa;
  166.     }    
  167.     if (_Y3<_Y1)              // switch. They're in the wrong order.
  168.     {
  169.         tempx=_X1; tempy=_Y1; tempa=_A1;
  170.         _X1=_X3;    _Y1=_Y3;    _A1=_A3;
  171.         _X3=tempx; _Y3=tempy; _A3=tempa;
  172.     }
  173.     if (_Y3<_Y2)              // switch. They're in the wrong order.
  174.     {
  175.         tempx=_X2; tempy=_Y2; tempa=_A2;
  176.         _X2=_X3;    _Y2=_Y3;    _A2=_A3;
  177.         _X3=tempx; _Y3=tempy; _A3=tempa;
  178.     }
  179.  
  180.     if (_Y3<_MinClipY || _Y1>_MaxClipY ||
  181.        (_X1<_MinClipX && _X2<_MinClipX && _X3<_MinClipX) ||
  182.        (_X1>_MaxClipX && _X2>_MaxClipX && _X3>_MaxClipX) )
  183.        return;              // do trivial rejection.
  184.         
  185.                             // now ALL vertices are in correct Counter-Clockwise order.
  186.  
  187.     GENERAL=FALSE;          // reset cases (for Triangle).
  188.     Buffer=_RendBuffer;     // save starting point of Buffer.
  189.       
  190.     if (_Y1==_Y2)
  191.       goto FLAT_TOP;
  192.     if (_Y2==_Y3)
  193.       goto FLAT_BOTTOM;
  194.     else
  195.       GENERAL=TRUE;
  196.          
  197.     height  = 65536/(_Y3-_Y1);
  198.     slope   = (_X3-_X1)*height;               
  199.     newx    = _X1+((slope*(_Y2-_Y1))>>16);
  200.     newa    = ( ((_Y2-_Y1)*_A3+(_Y3-_Y2)*_A1)*height )>>16;
  201.               
  202.     old_X3   = _X3;                            // save values for later.
  203.     old_Y3   = _Y3;
  204.     old_A3   = _A3;                        // save int3 for bottom half.
  205.  
  206.     _X3      = newx;
  207.     _Y3      = _Y2;           
  208.     _A3      = newa;
  209.        
  210.     FLAT_BOTTOM:
  211.  
  212.     if (_X3<_X2)
  213.     {
  214.       tempx=_X3; _X3=_X2; _X2=tempx;
  215.       tempa=_A3; _A3=_A2; _A2=tempa;
  216.     }
  217.  
  218.     height   = 65536/(_Y2-_Y1);
  219.     LeftDx   = (_X2-_X1)*height;    // Inverse left and right slope.
  220.     RightDx  = (_X3-_X1)*height;
  221.  
  222.     LeftX    = _X1<<16;              // assign Buffering coords.
  223.     RightX   = LeftX+32768;
  224.  
  225.     LeftA    = _A1<<16;              // assign angles to left and right side.
  226.     RightA   = LeftA;
  227.     MiddleA  = LeftA;        // Buffering from left angle.
  228.  
  229.     LeftDa   = (_A2 - _A1)*height;  // compute intensity deltas.
  230.     RightDa  = (_A3 - _A1)*height;
  231.  
  232.     ClipLeftX  = ( LeftX>>16 );              // just starting and ending x-coords
  233.     ClipRightX = ( RightX>>16 );
  234.  
  235.     MiddleDa = 0;
  236.       
  237.     if (_Y1 < _MinClipY)
  238.     {
  239.        ydiff    = (_MinClipY - _Y1);   
  240.        LeftX    = LeftX+LeftDx*ydiff;
  241.        RightX   = RightX+RightDx*ydiff;
  242.  
  243.        LeftA    = LeftA+LeftDa*ydiff;
  244.        RightA   = RightA+RightDa*ydiff;
  245.        MiddleA  = LeftA;
  246.              
  247.        ClipLeftX  = ( LeftX>>16 );              // just starting and ending x-coords
  248.        ClipRightX = ( RightX>>16 );
  249.       
  250.        MiddleDa = (RightA - LeftA)/( 1+ClipRightX-ClipLeftX );  // to avoid divide by 0.
  251.        
  252.            _Y1   = _MinClipY;
  253.     }
  254.                 
  255.     if (_Y3 > _MaxClipY)
  256.       _Y3 = _MaxClipY;
  257.               
  258.     Buffer+=(_Y1<<8)+(_Y1<<6);
  259.     
  260.     if (_X1>=_MinClipX && _X1<=_MaxClipX &&
  261.         _X2>=_MinClipX && _X2<=_MaxClipX &&
  262.         _X3>=_MinClipX && _X3<=_MaxClipX)
  263.     {                   
  264.        for (y=_Y1;y<_Y3;y++,Buffer+=SCREENWIDTH)
  265.        {
  266.           for (x=( ClipLeftX );x<=( ClipRightX );x++)
  267.           {
  268.              Buffer[x]=LookPalPhong[ _ColorIndex + PhongTBL[ ( MiddleA>>16 ) ] ];
  269.              MiddleA+=MiddleDa;
  270.           }
  271.         LeftA   += LeftDa;      // update the intensities and slopes.
  272.         RightA  += RightDa;
  273.         MiddleA  = LeftA;      
  274.         LeftX   += LeftDx;
  275.         RightX  += RightDx;
  276.         
  277.         ClipLeftX  = ( LeftX>>16 );
  278.         ClipRightX = ( RightX>>16 );
  279.       
  280.         MiddleDa = (RightA - LeftA)/( 1+ClipRightX-ClipLeftX );  // to avoid divide by 0.
  281.        }
  282.     }
  283.     else
  284.     {
  285.        for (y=_Y1;y<_Y3;y++,Buffer+=SCREENWIDTH)
  286.        {
  287.  
  288.           if (ClipLeftX < _MinClipX)
  289.           {
  290.             if (ClipRightX <= _MinClipX)
  291.             {
  292.                LeftX   += LeftDx;      // update the intensities and slopes.
  293.                RightX  += RightDx;     // before continuing.
  294.                LeftA   += LeftDa;      
  295.                RightA  += RightDa;         
  296.  
  297.                ClipLeftX  = ( LeftX>>16 );
  298.                ClipRightX = ( RightX>>16 );
  299.  
  300.                continue;
  301.             }
  302.             MiddleA = LeftA + (_MinClipX - ClipLeftX)*MiddleDa;   // move the intensity by  
  303.             ClipLeftX = _MinClipX;                                // the clipped amount.
  304.           }
  305.  
  306.           if (ClipRightX > _MaxClipX)
  307.           {
  308.             if (ClipLeftX > _MaxClipX)
  309.             {
  310.                LeftX   += LeftDx;      // update the intensities and slopes.
  311.                RightX  += RightDx;     // before continuing.
  312.                LeftA   += LeftDa;      
  313.                RightA  += RightDa;         
  314.  
  315.                ClipLeftX  = ( LeftX>>16 );
  316.                ClipRightX = ( RightX>>16 );
  317.  
  318.                continue;
  319.             }
  320.             ClipRightX = _MaxClipX;
  321.           }
  322.                       
  323.           for (x=( ClipLeftX );x<=( ClipRightX );x++)
  324.           {
  325.              Buffer[x]=LookPalPhong[ _ColorIndex + PhongTBL[ ( MiddleA>>16 ) ] ];
  326.              MiddleA  += MiddleDa;
  327.           }
  328.           LeftX   += LeftDx;
  329.           RightX  += RightDx;
  330.           LeftA   += LeftDa;   
  331.           RightA  += RightDa;         
  332.           MiddleA  = LeftA;      
  333.  
  334.           ClipLeftX  = ( LeftX>>16 );
  335.           ClipRightX = ( RightX>>16 );
  336.           
  337.           MiddleDa = (RightA - LeftA)/( 1+ClipRightX-ClipLeftX );  // to avoid divide by 0.
  338.        }
  339.     }
  340.  
  341.     if (!GENERAL)
  342.       return;
  343.  
  344.     _X1     = _X2;                 // setup for FLAT_TOP.
  345.     _Y1     = _Y2;
  346.     _A1     = _A2;
  347.  
  348.     _X2     = _X3;
  349.     _A2     = _A3;
  350.  
  351.     _X3     = old_X3;
  352.     _Y3     = old_Y3;
  353.     _A3     = old_A3;
  354.     
  355.     Buffer=_RendBuffer;     // save starting point of Buffer.
  356.     
  357.     FLAT_TOP: 
  358.  
  359.     if (_X2<_X1)
  360.     {
  361.       tempx=_X2; _X2=_X1; _X1=tempx;
  362.       tempa=_A2; _A2=_A1; _A1=tempa;
  363.     }
  364.  
  365.     height   = 65536/(_Y3-_Y1);
  366.     LeftDx   = (_X3-_X1)*height;    // Inverse left and right slope.
  367.     RightDx  = (_X3-_X2)*height;
  368.  
  369.     LeftX    = _X1<<16;              // assign Buffering coords.
  370.     RightX   = (_X2<<16)+32768;
  371.  
  372.     LeftA    = _A1<<16;              // assign intensity to left and right side.
  373.     RightA   = _A2<<16;
  374.     MiddleA  = LeftA;        // Buffering from left intensity.
  375.  
  376.     LeftDa   = (_A3 - _A1)*height;  // compute intensity deltas.
  377.     RightDa  = (_A3 - _A2)*height;
  378.  
  379.     ClipLeftX  = ( LeftX>>16 );
  380.     ClipRightX = ( RightX>>16 );
  381.  
  382.     MiddleDa = (RightA - LeftA)/( 1+ClipRightX-ClipLeftX );  // to avoid divide by 0.
  383.                          
  384.     if (_Y1 < _MinClipY)
  385.     {
  386.         ydiff   = (_MinClipY - _Y1);
  387.         LeftX   = LeftX+LeftDx*ydiff;
  388.         RightX  = RightX+RightDx*ydiff;
  389.  
  390.        LeftA    = LeftA+LeftDa*ydiff;
  391.        RightA   = RightA+RightDa*ydiff;
  392.        MiddleA  = LeftA;
  393.        
  394.        ClipLeftX  = ( LeftX>>16 );
  395.        ClipRightX = ( RightX>>16 );
  396.  
  397.        MiddleDa = (RightA - LeftA)/( 1+ClipRightX-ClipLeftX );  // to avoid divide by 0.
  398.        
  399.             _Y1  = _MinClipY;
  400.     }
  401.                 
  402.     if (_Y3 > _MaxClipY)
  403.       _Y3 = _MaxClipY;
  404.               
  405.     Buffer+=(_Y1<<8)+(_Y1<<6);
  406.  
  407.     if (_X1>=_MinClipX && _X1<=_MaxClipX &&
  408.         _X2>=_MinClipX && _X2<=_MaxClipX &&
  409.         _X3>=_MinClipX && _X3<=_MaxClipX)
  410.     {                         
  411.        for (y=_Y1;y<_Y3;y++,Buffer+=SCREENWIDTH)
  412.        {          
  413.           for (x=( ClipLeftX );x<=( ClipRightX );x++)
  414.           {
  415.              Buffer[x]=LookPalPhong[ _ColorIndex + PhongTBL[ ( MiddleA>>16 ) ] ];
  416.              MiddleA += MiddleDa;
  417.           }               
  418.         LeftA   += LeftDa;      // update the intensities and slopes.
  419.         RightA  += RightDa;
  420.         MiddleA  = LeftA;      
  421.         LeftX   += LeftDx;
  422.         RightX  += RightDx;
  423.  
  424.         ClipLeftX  = ( LeftX>>16 );
  425.         ClipRightX = ( RightX>>16 );
  426.       
  427.         MiddleDa = (RightA - LeftA)/( 1+ClipRightX-ClipLeftX );  // to avoid divide by 0.
  428.        }
  429.     }
  430.     else
  431.     {
  432.        for (y=_Y1;y<_Y3;y++,Buffer+=SCREENWIDTH)    
  433.        {
  434.  
  435.           if (ClipLeftX < _MinClipX)
  436.           {
  437.             if (ClipRightX <= _MinClipX)
  438.             {
  439.                LeftX   += LeftDx;      // update the intensities and slopes.
  440.                RightX  += RightDx;     // before continuing.
  441.                LeftA   += LeftDa;      
  442.                RightA  += RightDa;         
  443.  
  444.                ClipLeftX  = ( LeftX>>16 );
  445.                ClipRightX = ( RightX>>16 );
  446.  
  447.                continue;
  448.             }
  449.             MiddleA = LeftA + (_MinClipX - ClipLeftX)*MiddleDa;   // move the intensity by  
  450.             ClipLeftX = _MinClipX;                                // the clipped amount.
  451.           }
  452.  
  453.           if (ClipRightX > _MaxClipX)
  454.           {
  455.             if (ClipLeftX > _MaxClipX)
  456.             {
  457.                LeftX   += LeftDx;      // update the intensities and slopes.
  458.                RightX  += RightDx;     // before continuing.
  459.                LeftA   += LeftDa;      
  460.                RightA  += RightDa;         
  461.  
  462.                ClipLeftX  = ( LeftX>>16 );
  463.                ClipRightX = ( RightX>>16 );
  464.  
  465.                continue;
  466.             }
  467.             ClipRightX = _MaxClipX;
  468.           }
  469.                                             
  470.           for (x=ClipLeftX;x<=ClipRightX;x++)
  471.           {
  472.              Buffer[x]=LookPalPhong[ _ColorIndex + PhongTBL[ ( MiddleA>>16 ) ] ];
  473.              MiddleA  += MiddleDa;
  474.           }               
  475.           LeftX   += LeftDx;
  476.           RightX  += RightDx;
  477.           LeftA   += LeftDa;     
  478.           RightA  += RightDa;         
  479.           MiddleA  = LeftA;      
  480.  
  481.           ClipLeftX     = ( LeftX>>16 );
  482.           ClipRightX    = ( RightX>>16 );
  483.  
  484.           MiddleDa = (RightA - LeftA)/( 1+ClipRightX-ClipLeftX );  // to avoid divide by 0.
  485.        }
  486.     }           
  487. }
  488.  
  489. void Scan_Convert_TextureL(void)
  490. {
  491.   long LeftX, RightX, LeftDx, RightDx;
  492.   long u,v,ScanU,ScanV,LeftU,LeftV,RightU,RightV;
  493.   long LeftDu,LeftDv,RightDu,RightDv;
  494.   long width,height,slope;
  495.   int ClipLeftX,ClipRightX,ydiff;
  496.   int x,y,newx,newu,newv,tempx,tempy,tempu,tempv;
  497.   int old_X3,old_Y3,old_U3,old_V3;
  498.   int GENERAL;
  499.   unsigned char *Buffer;
  500.   
  501.     if ( (_X1==_X2 && _X2==_X3) || (_Y1==_Y2 && _Y2==_Y3) )
  502.       return;               // degenerated into lines.       
  503.  
  504.     if (_Y2<_Y1)              // switch. They're in the wrong order.
  505.     {
  506.         tempx=_X1; tempy=_Y1;
  507.         _X1=_X2;    _Y1=_Y2;
  508.         _X2=tempx; _Y2=tempy;
  509.  
  510.         tempu=_U1; tempv=_V1;
  511.         _U1=_U2;    _V1=_V2;
  512.         _U2=tempu; _V2=tempv;
  513.     }    
  514.     if (_Y3<_Y1)              // switch. They're in the wrong order.
  515.     {
  516.         tempx=_X1; tempy=_Y1;
  517.         _X1=_X3;    _Y1=_Y3;
  518.         _X3=tempx; _Y3=tempy;
  519.  
  520.         tempu=_U1; tempv=_V1;
  521.         _U1=_U3;    _V1=_V3;
  522.         _U3=tempu; _V3=tempv;
  523.     }
  524.     if (_Y3<_Y2)              // switch. They're in the wrong order.
  525.     {
  526.         tempx=_X2; tempy=_Y2;
  527.         _X2=_X3;    _Y2=_Y3;
  528.         _X3=tempx; _Y3=tempy;
  529.  
  530.         tempu=_U2; tempv=_V2;
  531.         _U2=_U3;    _V2=_V3;
  532.         _U3=tempu; _V3=tempv;
  533.     }    
  534.  
  535.     if (_Y3<_MinClipY || _Y1>_MaxClipY ||
  536.        (_X1<_MinClipX && _X2<_MinClipX && _X3<_MinClipX) ||
  537.        (_X1>_MaxClipX && _X2>_MaxClipX && _X3>_MaxClipX) )
  538.        return;              // do trivial rejection.
  539.         
  540.     GENERAL=FALSE;          // reset cases (for Triangle).
  541.     Buffer=_RendBuffer;     // save starting point of Buffer.
  542.     
  543.     if (_Y1==_Y2)
  544.       goto FLAT_TOP;
  545.     if (_Y2==_Y3)
  546.       goto FLAT_BOTTOM;
  547.     else
  548.       GENERAL=TRUE;
  549.          
  550.     height = 65536/(_Y3-_Y1);
  551.     slope = (_X3-_X1)*height;               
  552.     newx  = _X1+( (slope*(_Y2-_Y1))>>16 );
  553.     newu  = ( ((_Y2-_Y1)*_U3+(_Y3-_Y2)*_U1)*height )>>16;
  554.     newv  = ( ((_Y2-_Y1)*_V3+(_Y3-_Y2)*_V1)*height )>>16;
  555.     
  556.     old_X3 = _X3;                            // save values for later.
  557.     old_Y3 = _Y3;
  558.     old_U3 = _U3;
  559.     old_V3 = _V3;
  560.     
  561.     _X3    = newx;
  562.     _Y3    = _Y2;           
  563.     _U3    = newu;
  564.     _V3    = newv;
  565.     
  566.     FLAT_BOTTOM:
  567.         
  568.     if (_X3<_X2)
  569.     {
  570.       tempx=_X3; _X3=_X2; _X2=tempx;
  571.       tempu=_U3; _U3=_U2; _U2=tempu;
  572.       tempv=_V3; _V3=_V2; _V2=tempv;
  573.     }
  574.  
  575.     height  = 65536/(_Y2-_Y1);
  576.     LeftDx  = (_X2-_X1)*height;    // Inverse left and right slope.
  577.     RightDx = (_X3-_X1)*height;
  578.     LeftDu  = (_U2-_U1)*height;
  579.     LeftDv  = (_V2-_V1)*height;
  580.     RightDu = (_U3-_U1)*height;
  581.     RightDv = (_V3-_V1)*height;
  582.  
  583.     LeftX   = _X1<<16;
  584.     RightX  = LeftX+32768;
  585.     LeftU   = _U1<<16;
  586.     LeftV   = _V1<<16;
  587.     RightU  = LeftU;
  588.     RightV  = LeftV;
  589.     ScanU  =  0;
  590.     ScanV  =  0;
  591.  
  592.     ClipLeftX     = ( LeftX>>16 );
  593.     ClipRightX    = ( RightX>>16 );
  594.  
  595.     if (_Y1 < _MinClipY)
  596.     {
  597.        ydiff  = (_MinClipY - _Y1);
  598.        LeftX  = LeftX+LeftDx*ydiff;
  599.        RightX = RightX+RightDx*ydiff;
  600.  
  601.        LeftU  = LeftU+LeftDu*ydiff;
  602.        RightU = RightU+RightDu*ydiff;
  603.        LeftV  = LeftV+LeftDv*ydiff;
  604.        RightV = RightV+RightDv*ydiff;
  605.  
  606.        ClipLeftX  = ( LeftX>>16 );
  607.        ClipRightX = ( RightX>>16 );
  608.        width      = 1+ClipRightX-ClipLeftX;
  609.         
  610.        ScanU  =  (RightU-LeftU)/width;
  611.        ScanV  =  (RightV-LeftV)/width;            
  612.  
  613.            _Y1 = _MinClipY;
  614.     }
  615.                 
  616.     if (_Y3 > _MaxClipY)
  617.       _Y3 = _MaxClipY;             
  618.  
  619.     Buffer+=(_Y1<<8)+(_Y1<<6);
  620.  
  621.     if (_X1>=_MinClipX && _X1<=_MaxClipX &&
  622.         _X2>=_MinClipX && _X2<=_MaxClipX &&
  623.         _X3>=_MinClipX && _X3<=_MaxClipX)
  624.     {                   
  625.        for (y=_Y1;y<_Y3;y++,Buffer+=SCREENWIDTH)    
  626.        {
  627.           u=LeftU;
  628.           v=LeftV;
  629.           for (x=( ClipLeftX );x<=( ClipRightX );x++)
  630.           {                 
  631.              Buffer[x]=LookPal[ TextureMap[ (u>>16)+( (v>>16)<<8) ] + _ColorIndex ];
  632.              u+=ScanU;
  633.              v+=ScanV;
  634.           }       
  635.           LeftX  += LeftDx;
  636.           RightX += RightDx;
  637.           LeftU  += LeftDu;
  638.           LeftV  += LeftDv;
  639.           RightU += RightDu;
  640.           RightV += RightDv;
  641.  
  642.           ClipLeftX  = ( LeftX>>16 );
  643.           ClipRightX = ( RightX>>16 );
  644.           width      = 1+ClipRightX-ClipLeftX;
  645.  
  646.           ScanU   = (RightU-LeftU)/width;
  647.           ScanV   = (RightV-LeftV)/width;            
  648.        }
  649.     }
  650.     else
  651.     {
  652.        for (y=_Y1;y<_Y3;y++,Buffer+=SCREENWIDTH)    
  653.        {
  654.  
  655.           u=LeftU;
  656.           v=LeftV;
  657.  
  658.           if (ClipLeftX < _MinClipX)
  659.           {
  660.             if (ClipRightX <= _MinClipX)
  661.             {
  662.                LeftX  += LeftDx;      // update the intensities and slopes.
  663.                RightX += RightDx;     // before continuing.
  664.                LeftU  += LeftDu;
  665.                LeftV  += LeftDv;
  666.                RightU += RightDu;
  667.                RightV += RightDv;
  668.  
  669.                ClipLeftX  = ( LeftX>>16 );
  670.                ClipRightX = ( RightX>>16 );
  671.  
  672.                continue;
  673.             }
  674.             u         = LeftU + (_MinClipX - ClipLeftX)*ScanU;
  675.             v         = LeftV + (_MinClipX - ClipLeftX)*ScanV;
  676.             ClipLeftX = _MinClipX;     // the clipped amount.
  677.           }
  678.  
  679.           if (ClipRightX > _MaxClipX)
  680.           {
  681.             if (ClipLeftX > _MaxClipX)
  682.             {
  683.                LeftX  += LeftDx;      // update the intensities and slopes.
  684.                RightX += RightDx;     // before continuing.
  685.                LeftU  += LeftDu;
  686.                LeftV  += LeftDv;
  687.                RightU += RightDu;
  688.                RightV += RightDv;
  689.  
  690.                ClipLeftX  = ( LeftX>>16 );
  691.                ClipRightX = ( RightX>>16 );
  692.  
  693.                continue;
  694.             }
  695.             ClipRightX = _MaxClipX;
  696.           }
  697.                                             
  698.           for (x=ClipLeftX;x<ClipRightX;x++)
  699.           {
  700.              Buffer[x]=LookPal[ TextureMap[ (u>>16)+( (v>>16)<<8) ] + _ColorIndex ];
  701.              u+=ScanU;
  702.              v+=ScanV;
  703.           }               
  704.           LeftX  += LeftDx;
  705.           RightX += RightDx;
  706.           LeftU  += LeftDu;
  707.           LeftV  += LeftDv;
  708.           RightU += RightDu;
  709.           RightV += RightDv;
  710.  
  711.           ClipLeftX  = ( LeftX>>16 );
  712.           ClipRightX = ( RightX>>16 );
  713.           width      = 1+ClipRightX-ClipLeftX;
  714.  
  715.           ScanU   = (RightU-LeftU)/width;
  716.           ScanV   = (RightV-LeftV)/width;            
  717.        }
  718.     }
  719.  
  720.     if (!GENERAL)
  721.       return;
  722.  
  723.     _X1     = _X2;                 // setup for FLAT_TOP.
  724.     _Y1     = _Y2;
  725.     _U1     = _U2;
  726.     _V1     = _V2;
  727.  
  728.     _X2     = _X3;
  729.     _U2     = _U3;
  730.     _V2     = _V3;
  731.  
  732.     _X3     = old_X3;
  733.     _Y3     = old_Y3;
  734.     _U3     = old_U3;
  735.     _V3     = old_V3;
  736.     
  737.     Buffer=_RendBuffer;     // save starting point of Buffer.
  738.     
  739.     FLAT_TOP: 
  740.  
  741.     if (_X2<_X1)
  742.     {
  743.       tempx=_X2; _X2=_X1; _X1=tempx;
  744.       tempu=_U2; _U2=_U1; _U1=tempu;
  745.       tempv=_V2; _V2=_V1; _V1=tempv;
  746.     }
  747.  
  748.     height    = 65536/(_Y3-_Y1);
  749.  
  750.     LeftDx    = (_X3-_X1)*height;    // Inverse left and right slope.
  751.     RightDx   = (_X3-_X2)*height;
  752.     LeftDu    = (_U3-_U1)*height;
  753.     LeftDv    = (_V3-_V1)*height;
  754.     RightDu   = (_U3-_U2)*height;
  755.     RightDv   = (_V3-_V2)*height;
  756.  
  757.     LeftX     = _X1<<16;
  758.     RightX    = (_X2<<16)+32768;
  759.     LeftU     = _U1<<16;
  760.     LeftV     = _V1<<16;
  761.     RightU    = _U2<<16;
  762.     RightV    = _V2<<16;
  763.  
  764.     ClipLeftX  = ( LeftX>>16 );
  765.     ClipRightX = ( RightX>>16 );
  766.     width     = 1+ClipRightX-ClipLeftX;
  767.  
  768.     ScanU     = (RightU-LeftU)/width;
  769.     ScanV     = (RightV-LeftV)/width;            
  770.                        
  771.     if (_Y1 < _MinClipY)
  772.     {
  773.        ydiff  = (_MinClipY - _Y1);   
  774.        LeftX  = LeftX+LeftDx*ydiff;
  775.        RightX = RightX+RightDx*ydiff;
  776.  
  777.        LeftU  = LeftU+LeftDu*ydiff;
  778.        RightU = RightU+RightDu*ydiff;
  779.        LeftV  = LeftV+LeftDv*ydiff;
  780.        RightV = RightV+RightDv*ydiff;
  781.  
  782.        ClipLeftX  = ( LeftX>>16 );
  783.        ClipRightX = ( RightX>>16 );
  784.        width     = 1+ClipRightX-ClipLeftX;
  785.  
  786.        ScanU  = (RightU-LeftU)/width;
  787.        ScanV  = (RightV-LeftV)/width;            
  788.  
  789.            _Y1 = _MinClipY;
  790.     }
  791.                 
  792.     if (_Y3 > _MaxClipY)
  793.       _Y3 = _MaxClipY;
  794.               
  795.     Buffer+=(_Y1<<8)+(_Y1<<6);
  796.  
  797.     if (_X1>=_MinClipX && _X1<=_MaxClipX &&
  798.         _X2>=_MinClipX && _X2<=_MaxClipX &&
  799.         _X3>=_MinClipX && _X3<=_MaxClipX)
  800.     {                   
  801.        for (y=_Y1;y<_Y3;y++,Buffer+=SCREENWIDTH)    
  802.        {        
  803.           u=LeftU;
  804.           v=LeftV;
  805.           for (x=( ClipLeftX );x<=( ClipRightX );x++)
  806.           {                 
  807.              Buffer[x]=LookPal[ TextureMap[ (u>>16)+( (v>>16)<<8) ] + _ColorIndex ];
  808.              u+=ScanU;
  809.              v+=ScanV;
  810.           }
  811.           LeftX  += LeftDx;
  812.           RightX += RightDx;
  813.           LeftU  += LeftDu;
  814.           LeftV  += LeftDv;
  815.           RightU += RightDu;
  816.           RightV += RightDv;
  817.  
  818.           ClipLeftX  = ( LeftX>>16 );
  819.           ClipRightX = ( RightX>>16 );
  820.           width      = 1+ClipRightX-ClipLeftX;
  821.  
  822.           ScanU   = (RightU-LeftU)/width;
  823.           ScanV   = (RightV-LeftV)/width;            
  824.        }
  825.     }
  826.     else
  827.     {
  828.        for (y=_Y1;y<_Y3;y++,Buffer+=SCREENWIDTH)    
  829.        {
  830.           u=LeftU;
  831.           v=LeftV;
  832.  
  833.           if (ClipLeftX < _MinClipX)
  834.           {
  835.             if (ClipRightX <= _MinClipX)
  836.             {
  837.                LeftX  += LeftDx;      // update the intensities and slopes.
  838.                RightX += RightDx;     // before continuing.
  839.                LeftU  += LeftDu;
  840.                LeftV  += LeftDv;
  841.                RightU += RightDu;
  842.                RightV += RightDv;
  843.  
  844.                ClipLeftX  = ( LeftX>>16 );
  845.                ClipRightX = ( RightX>>16 );
  846.  
  847.                continue;
  848.             }
  849.             u          = LeftU + (_MinClipX - ClipLeftX)*ScanU;
  850.             v          = LeftV + (_MinClipX - ClipLeftX)*ScanV;
  851.             ClipLeftX  = _MinClipX;   // the clipped amount.
  852.           }
  853.  
  854.           if (ClipRightX > _MaxClipX)
  855.           {
  856.             if (ClipLeftX > _MaxClipX)
  857.             {
  858.                LeftX  += LeftDx;      // update the intensities and slopes.
  859.                RightX += RightDx;     // before continuing.
  860.                LeftU  += LeftDu;
  861.                LeftV  += LeftDv;
  862.                RightU += RightDu;
  863.                RightV += RightDv;
  864.  
  865.                ClipLeftX  = ( LeftX>>16 );
  866.                ClipRightX = ( RightX>>16 );
  867.  
  868.                continue;
  869.             }
  870.             ClipRightX = _MaxClipX;
  871.           }
  872.                                             
  873.           for (x=ClipLeftX;x<ClipRightX;x++)
  874.           {
  875.              Buffer[x]=LookPal[ TextureMap[ (u>>16)+( (v>>16)<<8) ] + _ColorIndex ];
  876.              u+=ScanU;
  877.              v+=ScanV;
  878.           }               
  879.           LeftX  += LeftDx;
  880.           RightX += RightDx;
  881.           LeftU  += LeftDu;
  882.           LeftV  += LeftDv;
  883.           RightU += RightDu;
  884.           RightV += RightDv;
  885.  
  886.           ClipLeftX  = ( LeftX>>16 );
  887.           ClipRightX = ( RightX>>16 );
  888.           width      = 1+ClipRightX-ClipLeftX;
  889.  
  890.           ScanU   = (RightU-LeftU)/width;
  891.           ScanV   = (RightV-LeftV)/width;            
  892.        }
  893.     }
  894. }
  895.  
  896. void Scan_Convert_TextureG(void)
  897. {
  898.   long LeftX, RightX, LeftDx, RightDx;
  899.   long u,v,ScanU,ScanV,LeftU,LeftV,RightU,RightV,LeftI,RightI,MiddleI;
  900.   long LeftDu,LeftDv,RightDu,RightDv,LeftDi,RightDi,MiddleDi;
  901.   long height,width,slope,ydiff;
  902.   int ClipLeftX,ClipRightX;
  903.   int x,y,newi,newx,newu,newv,tempx,tempy,tempu,tempv,tempi;
  904.   int old_X3,old_Y3,old_U3,old_V3,old_I3;
  905.   int GENERAL;
  906.   unsigned char *Buffer;
  907.   
  908.     if ( (_X1==_X2 && _X2==_X3) || (_Y1==_Y2 && _Y2==_Y3) )
  909.       return;               // degenerated into lines.       
  910.  
  911.     if (_Y2<_Y1)              // switch. They're in the wrong order.
  912.     {
  913.         tempx=_X1; tempy=_Y1;
  914.         _X1=_X2;    _Y1=_Y2;
  915.         _X2=tempx; _Y2=tempy;
  916.  
  917.         tempu=_U1; tempv=_V1; tempi=_I1;
  918.         _U1=_U2;    _V1=_V2;    _I1=_I2;
  919.         _U2=tempu; _V2=tempv; _I2=tempi;
  920.     }    
  921.     if (_Y3<_Y1)              // switch. They're in the wrong order.
  922.     {
  923.         tempx=_X1; tempy=_Y1;
  924.         _X1=_X3;    _Y1=_Y3;
  925.         _X3=tempx; _Y3=tempy;
  926.  
  927.         tempu=_U1; tempv=_V1; tempi=_I1;
  928.         _U1=_U3;    _V1=_V3;    _I1=_I3;
  929.         _U3=tempu; _V3=tempv; _I3=tempi;
  930.     }
  931.     if (_Y3<_Y2)              // switch. They're in the wrong order.
  932.     {
  933.         tempx=_X2; tempy=_Y2;
  934.         _X2=_X3;    _Y2=_Y3;
  935.         _X3=tempx; _Y3=tempy;
  936.  
  937.         tempu=_U2; tempv=_V2; tempi=_I2;
  938.         _U2=_U3;    _V2=_V3;    _I2=_I3;
  939.         _U3=tempu; _V3=tempv; _I3=tempi;
  940.     }    
  941.  
  942.     if (_Y3<_MinClipY || _Y1>_MaxClipY ||
  943.        (_X1<_MinClipX && _X2<_MinClipX && _X3<_MinClipX) ||
  944.        (_X1>_MaxClipX && _X2>_MaxClipX && _X3>_MaxClipX) )
  945.        return;              // do trivial rejection.
  946.         
  947.     GENERAL=FALSE;          // reset cases (for Triangle).
  948.     Buffer=_RendBuffer;     // save starting point of Buffer.
  949.     
  950.     if (_Y1==_Y2)
  951.       goto FLAT_TOP;
  952.     if (_Y2==_Y3)
  953.       goto FLAT_BOTTOM;
  954.     else
  955.       GENERAL=TRUE;
  956.          
  957.     height  = 65536/(_Y3-_Y1);
  958.     slope   = (_X3-_X1)*height;               
  959.     newx    = _X1+( (slope*(_Y2-_Y1))>>16 );
  960.     newu    = (((_Y2-_Y1)*_U3+(_Y3-_Y2)*_U1)*height)>>16;
  961.     newv    = (((_Y2-_Y1)*_V3+(_Y3-_Y2)*_V1)*height)>>16;
  962.     newi    = (((_Y2-_Y1)*_I3+(_Y3-_Y2)*_I1)*height)>>16;
  963.  
  964.     old_X3   = _X3;                            // save values for later.
  965.     old_Y3   = _Y3;
  966.     old_U3   = _U3;
  967.     old_V3   = _V3;
  968.     old_I3   = _I3;
  969.     
  970.     _X3      = newx;
  971.     _Y3      = _Y2;           
  972.     _U3      = newu;
  973.     _V3      = newv;
  974.     _I3      = newi;
  975.     
  976.     FLAT_BOTTOM:
  977.  
  978.     if (_X3<_X2)
  979.     {
  980.       tempx=_X3; _X3=_X2; _X2=tempx;
  981.       tempu=_U3; _U3=_U2; _U2=tempu;
  982.       tempv=_V3; _V3=_V2; _V2=tempv;
  983.       tempi=_I3; _I3=_I2; _I2=tempi;
  984.     }
  985.  
  986.     height  = 65536/(_Y3-_Y1);
  987.     LeftDx  = (_X2-_X1)*height;    // Inverse left and right slope.
  988.     RightDx = (_X3-_X1)*height;
  989.     LeftDu  = (_U2-_U1)*height;
  990.     LeftDv  = (_V2-_V1)*height;
  991.     RightDu = (_U3-_U1)*height;
  992.     RightDv = (_V3-_V1)*height;
  993.     LeftDi  = (_I2-_I1)*height;  // compute intensity deltas.
  994.     RightDi = (_I3-_I1)*height; 
  995.  
  996.     LeftX   = _X1<<16;
  997.     RightX  = LeftX+32768;
  998.     LeftU   = _U1<<16;
  999.     LeftV   = _V1<<16;
  1000.     RightU  = LeftU;
  1001.     RightV  = LeftV;
  1002.     LeftI   = _I1<<16;              // assign intensity to left and right side.
  1003.     RightI  = LeftI;
  1004.  
  1005.     ScanU    = 0;              // because of Flat bottom.
  1006.     ScanV    = 0;
  1007.     MiddleDi = 0;
  1008.  
  1009.     ClipLeftX  = ( LeftX>>16 );
  1010.     ClipRightX = ( RightX>>16 );
  1011.  
  1012.     if (_Y1 < _MinClipY)
  1013.     {
  1014.        ydiff    = (_MinClipY - _Y1);
  1015.        LeftX    = LeftX+LeftDx*ydiff;
  1016.        RightX   = RightX+RightDx*ydiff;
  1017.  
  1018.        LeftU    = LeftU+LeftDu*ydiff;
  1019.        RightU   = RightU+RightDu*ydiff;
  1020.        LeftV    = LeftV+LeftDv*ydiff;
  1021.        RightV   = RightV+RightDv*ydiff;
  1022.  
  1023.        LeftI    = LeftI+LeftDi*ydiff;
  1024.        RightI   = RightI+RightDi*ydiff;
  1025.  
  1026.        ClipLeftX  = ( LeftX>>16 );
  1027.        ClipRightX = ( RightX>>16 );
  1028.        width    = 1+ClipRightX-ClipLeftX;
  1029.  
  1030.        ScanU    = (RightU-LeftU)/width;
  1031.        ScanV    = (RightV-LeftV)/width;            
  1032.        MiddleDi = (RightI-LeftI)/width;          
  1033.  
  1034.            _Y1 = _MinClipY;
  1035.     }
  1036.                 
  1037.     if (_Y3 > _MaxClipY)
  1038.       _Y3 = _MaxClipY;
  1039.               
  1040.     Buffer+=(_Y1<<8)+(_Y1<<6);
  1041.  
  1042.     if (_X1>=_MinClipX && _X1<=_MaxClipX &&
  1043.         _X2>=_MinClipX && _X2<=_MaxClipX &&
  1044.         _X3>=_MinClipX && _X3<=_MaxClipX)
  1045.     {                   
  1046.        for (y=_Y1;y<_Y3;y++,Buffer+=SCREENWIDTH)    
  1047.        {
  1048.           u=LeftU;
  1049.           v=LeftV;
  1050.           MiddleI=LeftI;
  1051.           for (x=( ClipLeftX );x<=( ClipRightX );x++)
  1052.           {                 
  1053.              Buffer[x]=LookPal[ TextureMap[ (u>>16)+( (v>>16)<<8) ] + (MiddleI>>16) ];
  1054.              u+=ScanU;
  1055.              v+=ScanV;
  1056.              MiddleI+=MiddleDi;
  1057.           }       
  1058.           LeftX   += LeftDx;
  1059.           RightX  += RightDx;
  1060.           LeftU   += LeftDu;
  1061.           LeftV   += LeftDv;
  1062.           RightU  += RightDu;
  1063.           RightV  += RightDv;
  1064.           LeftI   += LeftDi;      // update the intensities and slopes.
  1065.           RightI  += RightDi;
  1066.  
  1067.           ClipLeftX  = ( LeftX>>16 );
  1068.           ClipRightX = ( RightX>>16 );
  1069.           width      = 1+ClipRightX-ClipLeftX;
  1070.  
  1071.           ScanU    = (RightU-LeftU)/width;
  1072.           ScanV    = (RightV-LeftV)/width;            
  1073.           MiddleDi = (RightI-LeftI)/width;          
  1074.        }
  1075.     }
  1076.     else
  1077.     {
  1078.        for (y=_Y1;y<_Y3;y++,Buffer+=SCREENWIDTH)    
  1079.        {
  1080.           u=LeftU;
  1081.           v=LeftV;
  1082.           MiddleI=LeftI;                                            
  1083.  
  1084.           if (ClipLeftX < _MinClipX)
  1085.           {
  1086.             if (ClipRightX <= _MinClipX)
  1087.             {
  1088.                LeftX  += LeftDx;      // update the intensities and slopes.
  1089.                RightX += RightDx;     // before continuing.
  1090.                LeftU  += LeftDu;
  1091.                LeftV  += LeftDv;
  1092.                RightU += RightDu;
  1093.                RightV += RightDv;
  1094.                LeftI   += LeftDi;      
  1095.                RightI  += RightDi;         
  1096.  
  1097.                ClipLeftX  = ( LeftX>>16 );
  1098.                ClipRightX = ( RightX>>16 );
  1099.  
  1100.                continue;
  1101.             }
  1102.             u         = LeftU + (_MinClipX - ClipLeftX)*ScanU;
  1103.             v         = LeftV + (_MinClipX - ClipLeftX)*ScanV;
  1104.             MiddleI   = LeftI + (_MinClipX - ClipLeftX)*MiddleDi;   // move the intensity by  
  1105.             ClipLeftX = _MinClipX;     // the clipped amount.
  1106.           }
  1107.  
  1108.           if (ClipRightX > _MaxClipX)
  1109.           {
  1110.             if (ClipLeftX > _MaxClipX)
  1111.             {
  1112.                LeftX  += LeftDx;      // update the intensities and slopes.
  1113.                RightX += RightDx;     // before continuing.
  1114.                LeftU  += LeftDu;
  1115.                LeftV  += LeftDv;
  1116.                RightU += RightDu;
  1117.                RightV += RightDv;
  1118.                LeftI  += LeftDi;      
  1119.                RightI += RightDi;         
  1120.  
  1121.                ClipLeftX  = ( LeftX>>16 );
  1122.                ClipRightX = ( RightX>>16 );
  1123.  
  1124.                continue;
  1125.             }
  1126.             ClipRightX = _MaxClipX;
  1127.           }
  1128.           
  1129.           for (x=ClipLeftX;x<ClipRightX;x++)
  1130.           {
  1131.              Buffer[x]=LookPal[ TextureMap[ (u>>16)+( (v>>16)<<8) ] + (MiddleI>>16) ];
  1132.              u+=ScanU;
  1133.              v+=ScanV;
  1134.              MiddleI+=MiddleDi;
  1135.           }               
  1136.           LeftX   += LeftDx;
  1137.           RightX  += RightDx;
  1138.           LeftU   += LeftDu;
  1139.           LeftV   += LeftDv;
  1140.           RightU  += RightDu;
  1141.           RightV  += RightDv;
  1142.           LeftI   += LeftDi;   
  1143.           RightI  += RightDi;         
  1144.  
  1145.           ClipLeftX  = ( LeftX>>16 );
  1146.           ClipRightX = ( RightX>>16 );
  1147.           width      = 1+ClipRightX-ClipLeftX;
  1148.  
  1149.           ScanU    = (RightU-LeftU)/width;
  1150.           ScanV    = (RightV-LeftV)/width;            
  1151.           MiddleDi = (RightI-LeftI)/width;          
  1152.        }
  1153.     }
  1154.  
  1155.     if (!GENERAL)
  1156.       return;
  1157.  
  1158.     _X1 = _X2;                 // setup for FLAT_TOP.
  1159.     _Y1 = _Y2;
  1160.     _U1 = _U2;
  1161.     _V1 = _V2;
  1162.     _I1 = _I2;
  1163.  
  1164.     _X2 = _X3;
  1165.     _U2 = _U3;
  1166.     _V2 = _V3;
  1167.     _I2 = _I3;
  1168.  
  1169.     _X3 = old_X3;
  1170.     _Y3 = old_Y3;
  1171.     _U3 = old_U3;
  1172.     _V3 = old_V3;
  1173.     _I3 = old_I3;
  1174.     
  1175.     Buffer=_RendBuffer;     // save starting point of Buffer.
  1176.     
  1177.     FLAT_TOP: 
  1178.  
  1179.     if (_X2<_X1)
  1180.     {
  1181.       tempx=_X2; _X2=_X1; _X1=tempx;
  1182.       tempu=_U2; _U2=_U1; _U1=tempu;
  1183.       tempv=_V2; _V2=_V1; _V1=tempv;
  1184.       tempi=_I2; _I2=_I1; _I1=tempi;
  1185.     }
  1186.  
  1187.     height    = 65536/(_Y3-_Y1);
  1188.  
  1189.     LeftDx    = (_X3-_X1)*height;    // Inverse left and right slope.
  1190.     RightDx   = (_X3-_X2)*height;
  1191.     LeftDu    = (_U3-_U1)*height;
  1192.     LeftDv    = (_V3-_V1)*height;
  1193.     RightDu   = (_U3-_U2)*height;
  1194.     RightDv   = (_V3-_V2)*height;
  1195.     LeftDi    = (_I3-_I1)*height;  // compute intensity deltas.
  1196.     RightDi   = (_I3-_I2)*height; 
  1197.  
  1198.     LeftX     = _X1<<16;
  1199.     RightX    = (_X2<<16)+32768;
  1200.     LeftU     = _U1<<16;
  1201.     LeftV     = _V1<<16;
  1202.     RightU    = _U2<<16;
  1203.     RightV    = _V2<<16;
  1204.     LeftI     = _I1<<16;              // assign intensity to left and right side.
  1205.     RightI    = _I2<<16;
  1206.  
  1207.      ClipLeftX  = ( LeftX>>16 );
  1208.      ClipRightX = ( RightX>>16 );
  1209.      width     = 1+ClipRightX-ClipLeftX;
  1210.      
  1211.      ScanU    = (RightU-LeftU)/width;
  1212.      ScanV    = (RightV-LeftV)/width;            
  1213.      MiddleDi = (RightI-LeftI)/width;          
  1214.                        
  1215.     if (_Y1 < _MinClipY)
  1216.     {
  1217.        ydiff    = (_MinClipY - _Y1);
  1218.        LeftX    = LeftX+LeftDx*ydiff;
  1219.        RightX   = RightX+RightDx*ydiff;
  1220.  
  1221.        LeftU    = LeftU+LeftDu*ydiff;
  1222.        RightU   = RightU+RightDu*ydiff;
  1223.        LeftV    = LeftV+LeftDv*ydiff;
  1224.        RightV   = RightV+RightDv*ydiff;
  1225.  
  1226.        LeftI    = LeftI+LeftDi*ydiff;
  1227.        RightI   = RightI+RightDi*ydiff;
  1228.  
  1229.        ClipLeftX  = ( LeftX>>16 );
  1230.        ClipRightX = ( RightX>>16 );
  1231.        width     = 1+ClipRightX-ClipLeftX;
  1232.      
  1233.        ScanU    = (RightU-LeftU)/width;
  1234.        ScanV    = (RightV-LeftV)/width;            
  1235.        MiddleDi = (RightI-LeftI)/width;          
  1236.                        
  1237.              _Y1 = _MinClipY;
  1238.     }
  1239.                 
  1240.     if (_Y3 > _MaxClipY)
  1241.       _Y3 = _MaxClipY;
  1242.               
  1243.     Buffer+=(_Y1<<8)+(_Y1<<6);
  1244.  
  1245.     if (_X1>=_MinClipX && _X1<=_MaxClipX &&
  1246.         _X2>=_MinClipX && _X2<=_MaxClipX &&
  1247.         _X3>=_MinClipX && _X3<=_MaxClipX)
  1248.     {                   
  1249.        for (y=_Y1;y<_Y3;y++,Buffer+=SCREENWIDTH)    
  1250.        {        
  1251.           u=LeftU;
  1252.           v=LeftV;
  1253.           MiddleI=LeftI;
  1254.           for (x=( ClipLeftX );x<=( ClipRightX );x++)
  1255.           {                 
  1256.              Buffer[x]=LookPal[ TextureMap[ (u>>16)+( (v>>16)<<8) ] + (MiddleI>>16) ];
  1257.              u+=ScanU;
  1258.              v+=ScanV;
  1259.              MiddleI+=MiddleDi;
  1260.           }
  1261.           LeftX   += LeftDx;
  1262.           RightX  += RightDx;
  1263.           LeftU   += LeftDu;
  1264.           LeftV   += LeftDv;
  1265.           RightU  += RightDu;
  1266.           RightV  += RightDv;
  1267.           LeftI   += LeftDi;      // update the intensities and slopes.
  1268.           RightI  += RightDi;
  1269.  
  1270.           ClipLeftX  = ( LeftX>>16 );
  1271.           ClipRightX = ( RightX>>16 );
  1272.           width      = 1+ClipRightX-ClipLeftX;
  1273.      
  1274.           ScanU    = (RightU-LeftU)/width;
  1275.           ScanV    = (RightV-LeftV)/width;            
  1276.           MiddleDi = (RightI-LeftI)/width;          
  1277.        }
  1278.     }
  1279.     else
  1280.     {
  1281.        for (y=_Y1;y<_Y3;y++,Buffer+=SCREENWIDTH)
  1282.        {
  1283.  
  1284.           u=LeftU;
  1285.           v=LeftV;
  1286.           MiddleI=LeftI;
  1287.           if (ClipLeftX < _MinClipX)
  1288.           {
  1289.             if (ClipRightX <= _MinClipX)
  1290.             {
  1291.                LeftX  += LeftDx;      // update the intensities and slopes.
  1292.                RightX += RightDx;     // before continuing.
  1293.                LeftU  += LeftDu;
  1294.                LeftV  += LeftDv;
  1295.                RightU += RightDu;
  1296.                RightV += RightDv;
  1297.                LeftI  += LeftDi;      
  1298.                RightI += RightDi;         
  1299.  
  1300.                ClipLeftX  = ( LeftX>>16 );
  1301.                ClipRightX = ( RightX>>16 );
  1302.  
  1303.                continue;
  1304.             }
  1305.             u          = LeftU + (_MinClipX - ClipLeftX)*ScanU;
  1306.             v          = LeftV + (_MinClipX - ClipLeftX)*ScanV;
  1307.             MiddleI    = LeftI + (_MinClipX - ClipLeftX)*MiddleDi;   // move the intensity by
  1308.             ClipLeftX  = _MinClipX;   // the clipped amount.
  1309.           }
  1310.  
  1311.           if (ClipRightX > _MaxClipX)
  1312.           {
  1313.             if (ClipLeftX > _MaxClipX)
  1314.             {
  1315.                LeftX  += LeftDx;      // update the intensities and slopes.
  1316.                RightX += RightDx;     // before continuing.
  1317.                LeftU  += LeftDu;
  1318.                LeftV  += LeftDv;
  1319.                RightU += RightDu;
  1320.                RightV += RightDv;
  1321.                LeftI  += LeftDi;      
  1322.                RightI += RightDi;
  1323.  
  1324.                ClipLeftX  = ( LeftX>>16 );
  1325.                ClipRightX = ( RightX>>16 );
  1326.  
  1327.                continue;
  1328.             }
  1329.             ClipRightX = _MaxClipX;
  1330.           }
  1331.                                             
  1332.           for (x=ClipLeftX;x<ClipRightX;x++)
  1333.           {
  1334.              Buffer[x]=LookPal[ TextureMap[ (u>>16)+( (v>>16)<<8) ] + (MiddleI>>16) ];
  1335.              u+=ScanU;
  1336.              v+=ScanV;
  1337.              MiddleI+=MiddleDi;
  1338.           }               
  1339.           LeftX   += LeftDx;
  1340.           RightX  += RightDx;
  1341.           LeftU   += LeftDu;
  1342.           LeftV   += LeftDv;
  1343.           RightU  += RightDu;
  1344.           RightV  += RightDv;
  1345.           LeftI   += LeftDi;     
  1346.           RightI  += RightDi;         
  1347.  
  1348.           ClipLeftX  = ( LeftX>>16 );
  1349.           ClipRightX = ( RightX>>16 );
  1350.           width      = 1+ClipRightX-ClipLeftX;
  1351.      
  1352.           ScanU    = (RightU-LeftU)/width;
  1353.           ScanV    = (RightV-LeftV)/width;            
  1354.           MiddleDi = (RightI-LeftI)/width;          
  1355.        }
  1356.     }
  1357. }
  1358.  
  1359. void Scan_Convert_TextureP(void)
  1360. {
  1361.   long LeftX, RightX, LeftDx, RightDx;
  1362.   long u,v,ScanU,ScanV,LeftU,LeftV,RightU,RightV,LeftA,RightA,MiddleA;
  1363.   long LeftDu,LeftDv,RightDu,RightDv,LeftDa,RightDa,MiddleDa;
  1364.   long height,width,slope,ydiff;
  1365.   int ClipLeftX,ClipRightX;
  1366.   int x,y,newa,newx,newu,newv,tempx,tempy,tempu,tempv,tempa;
  1367.   int old_X3,old_Y3,old_U3,old_V3,old_A3;
  1368.   int GENERAL;
  1369.   unsigned char *Buffer;
  1370.   
  1371.     // NOTE: _A1,_A2,_A3 don't hold intensities but instead hold fixed point angles
  1372.     // between light and avgnormal vector. We interpolate the angles and then
  1373.     // index into a PhongTBL to get intensity at that angle. This amazing
  1374.     // algorithm was thought up by Zach Mortensen (Voltaire/OTM.) mortens1@nersc.gov.
  1375.     
  1376.     if ( (_X1==_X2 && _X2==_X3) || (_Y1==_Y2 && _Y2==_Y3) )
  1377.       return;               // degenerated into lines.       
  1378.  
  1379.     if (_Y2<_Y1)              // switch. They're in the wrong order.
  1380.     {
  1381.         tempx=_X1; tempy=_Y1;
  1382.         _X1=_X2;    _Y1=_Y2;
  1383.         _X2=tempx; _Y2=tempy;
  1384.  
  1385.         tempu=_U1; tempv=_V1; tempa=_A1;
  1386.         _U1=_U2;    _V1=_V2;    _A1=_A2;
  1387.         _U2=tempu; _V2=tempv; _A2=tempa;
  1388.     }    
  1389.     if (_Y3<_Y1)              // switch. They're in the wrong order.
  1390.     {
  1391.         tempx=_X1; tempy=_Y1;
  1392.         _X1=_X3;    _Y1=_Y3;
  1393.         _X3=tempx; _Y3=tempy;
  1394.  
  1395.         tempu=_U1; tempv=_V1; tempa=_A1;
  1396.         _U1=_U3;    _V1=_V3;    _A1=_A3;
  1397.         _U3=tempu; _V3=tempv; _A3=tempa;
  1398.     }
  1399.     if (_Y3<_Y2)              // switch. They're in the wrong order.
  1400.     {
  1401.         tempx=_X2; tempy=_Y2;
  1402.         _X2=_X3;    _Y2=_Y3;
  1403.         _X3=tempx; _Y3=tempy;
  1404.  
  1405.         tempu=_U2; tempv=_V2; tempa=_A2;
  1406.         _U2=_U3;    _V2=_V3;    _A2=_A3;
  1407.         _U3=tempu; _V3=tempv; _A3=tempa;
  1408.     }    
  1409.  
  1410.     if (_Y3<_MinClipY || _Y1>_MaxClipY ||
  1411.        (_X1<_MinClipX && _X2<_MinClipX && _X3<_MinClipX) ||
  1412.        (_X1>_MaxClipX && _X2>_MaxClipX && _X3>_MaxClipX) )
  1413.        return;              // do trivial rejection.
  1414.         
  1415.     GENERAL=FALSE;          // reset cases (for Triangle).
  1416.     Buffer=_RendBuffer;     // save starting point of Buffer.
  1417.     
  1418.     if (_Y1==_Y2)
  1419.       goto FLAT_TOP;
  1420.     if (_Y2==_Y3)
  1421.       goto FLAT_BOTTOM;
  1422.     else
  1423.       GENERAL=TRUE;
  1424.          
  1425.     height  = 65536/(_Y3-_Y1);
  1426.     slope   = (_X3-_X1)*height;               
  1427.     newx    = _X1+( (slope*(_Y2-_Y1))>>16 );
  1428.     newu    = (((_Y2-_Y1)*_U3+(_Y3-_Y2)*_U1)*height)>>16;
  1429.     newv    = (((_Y2-_Y1)*_V3+(_Y3-_Y2)*_V1)*height)>>16;
  1430.     newa    = (((_Y2-_Y1)*_A3+(_Y3-_Y2)*_A1)*height)>>16;
  1431.  
  1432.     old_X3   = _X3;                            // save values for later.
  1433.     old_Y3   = _Y3;
  1434.     old_U3   = _U3;
  1435.     old_V3   = _V3;
  1436.     old_A3   = _A3;
  1437.     
  1438.     _X3      = newx;
  1439.     _Y3      = _Y2;           
  1440.     _U3      = newu;
  1441.     _V3      = newv;
  1442.     _A3      = newa;
  1443.     
  1444.     FLAT_BOTTOM:
  1445.  
  1446.     if (_X3<_X2)
  1447.     {
  1448.       tempx=_X3; _X3=_X2; _X2=tempx;
  1449.       tempu=_U3; _U3=_U2; _U2=tempu;
  1450.       tempv=_V3; _V3=_V2; _V2=tempv;
  1451.       tempa=_A3; _A3=_A2; _A2=tempa;
  1452.     }
  1453.  
  1454.     height  = 65536/(_Y3-_Y1);
  1455.     LeftDx  = (_X2-_X1)*height;    // Inverse left and right slope.
  1456.     RightDx = (_X3-_X1)*height;
  1457.     LeftDu  = (_U2-_U1)*height;
  1458.     LeftDv  = (_V2-_V1)*height;
  1459.     RightDu = (_U3-_U1)*height;
  1460.     RightDv = (_V3-_V1)*height;
  1461.     LeftDa  = (_A2-_A1)*height;  // compute intensity deltas.
  1462.     RightDa = (_A3-_A1)*height; 
  1463.  
  1464.     LeftX   = _X1<<16;
  1465.     RightX  = LeftX+32768;
  1466.     LeftU   = _U1<<16;
  1467.     LeftV   = _V1<<16;
  1468.     RightU  = LeftU;
  1469.     RightV  = LeftV;
  1470.     LeftA   = _A1<<16;              // assign intensity to left and right side.
  1471.     RightA  = LeftA;
  1472.  
  1473.     ScanU    = 0;              // because of Flat bottom.
  1474.     ScanV    = 0;
  1475.     MiddleDa = 0;
  1476.  
  1477.     ClipLeftX  = ( LeftX>>16 );
  1478.     ClipRightX = ( RightX>>16 );
  1479.  
  1480.     if (_Y1 < _MinClipY)
  1481.     {
  1482.        ydiff    = (_MinClipY - _Y1);
  1483.        LeftX    = LeftX+LeftDx*ydiff;
  1484.        RightX   = RightX+RightDx*ydiff;
  1485.  
  1486.        LeftU    = LeftU+LeftDu*ydiff;
  1487.        RightU   = RightU+RightDu*ydiff;
  1488.        LeftV    = LeftV+LeftDv*ydiff;
  1489.        RightV   = RightV+RightDv*ydiff;
  1490.  
  1491.        LeftA    = LeftA+LeftDa*ydiff;
  1492.        RightA   = RightA+RightDa*ydiff;
  1493.  
  1494.        ClipLeftX  = ( LeftX>>16 );
  1495.        ClipRightX = ( RightX>>16 );
  1496.        width    = 1+ClipRightX-ClipLeftX;
  1497.  
  1498.        ScanU    = (RightU-LeftU)/width;
  1499.        ScanV    = (RightV-LeftV)/width;            
  1500.        MiddleDa = (RightA-LeftA)/width;          
  1501.  
  1502.            _Y1 = _MinClipY;
  1503.     }
  1504.                 
  1505.     if (_Y3 > _MaxClipY)
  1506.       _Y3 = _MaxClipY;
  1507.               
  1508.     Buffer+=(_Y1<<8)+(_Y1<<6);
  1509.  
  1510.     if (_X1>=_MinClipX && _X1<=_MaxClipX &&
  1511.         _X2>=_MinClipX && _X2<=_MaxClipX &&
  1512.         _X3>=_MinClipX && _X3<=_MaxClipX)
  1513.     {                   
  1514.        for (y=_Y1;y<_Y3;y++,Buffer+=SCREENWIDTH)    
  1515.        {
  1516.           u=LeftU;
  1517.           v=LeftV;
  1518.           MiddleA=LeftA;
  1519.           for (x=( ClipLeftX );x<=( ClipRightX );x++)
  1520.           {                 
  1521.              Buffer[x]=LookPalPhong[ TextureMap[ (u>>16)+( (v>>16)<<8) ] + PhongTBL[ (MiddleA>>16) ] ];
  1522.              u+=ScanU;
  1523.              v+=ScanV;
  1524.              MiddleA+=MiddleDa;
  1525.           }       
  1526.           LeftX   += LeftDx;
  1527.           RightX  += RightDx;
  1528.           LeftU   += LeftDu;
  1529.           LeftV   += LeftDv;
  1530.           RightU  += RightDu;
  1531.           RightV  += RightDv;
  1532.           LeftA   += LeftDa;      // update the intensities and slopes.
  1533.           RightA  += RightDa;
  1534.  
  1535.           ClipLeftX  = ( LeftX>>16 );
  1536.           ClipRightX = ( RightX>>16 );
  1537.           width      = 1+ClipRightX-ClipLeftX;
  1538.      
  1539.           ScanU    = (RightU-LeftU)/width;
  1540.           ScanV    = (RightV-LeftV)/width;            
  1541.           MiddleDa = (RightA-LeftA)/width;          
  1542.        }
  1543.     }
  1544.     else
  1545.     {
  1546.        for (y=_Y1;y<_Y3;y++,Buffer+=SCREENWIDTH)    
  1547.        {
  1548.           u=LeftU;
  1549.           v=LeftV;
  1550.           MiddleA=LeftA;                                            
  1551.  
  1552.           if (ClipLeftX < _MinClipX)
  1553.           {
  1554.             if (ClipRightX <= _MinClipX)
  1555.             {
  1556.                LeftX  += LeftDx;      // update the intensities and slopes.
  1557.                RightX += RightDx;     // before continuing.
  1558.                LeftU  += LeftDu;
  1559.                LeftV  += LeftDv;
  1560.                RightU += RightDu;
  1561.                RightV += RightDv;
  1562.                LeftA   += LeftDa;      
  1563.                RightA  += RightDa;         
  1564.  
  1565.                ClipLeftX  = ( LeftX>>16 );
  1566.                ClipRightX = ( RightX>>16 );
  1567.  
  1568.                continue;
  1569.             }
  1570.             u         = LeftU + (_MinClipX - ClipLeftX)*ScanU;
  1571.             v         = LeftV + (_MinClipX - ClipLeftX)*ScanV;
  1572.             MiddleA   = LeftA + (_MinClipX - ClipLeftX)*MiddleDa;   // move the intensity by
  1573.             ClipLeftX = _MinClipX;     // the clipped amount.
  1574.           }
  1575.  
  1576.           if (ClipRightX > _MaxClipX)
  1577.           {
  1578.             if (ClipLeftX > _MaxClipX)
  1579.             {
  1580.                LeftX  += LeftDx;      // update the intensities and slopes.
  1581.                RightX += RightDx;     // before continuing.
  1582.                LeftU  += LeftDu;
  1583.                LeftV  += LeftDv;
  1584.                RightU += RightDu;
  1585.                RightV += RightDv;
  1586.                LeftA  += LeftDa;      
  1587.                RightA += RightDa;         
  1588.  
  1589.                ClipLeftX  = ( LeftX>>16 );
  1590.                ClipRightX = ( RightX>>16 );
  1591.  
  1592.                continue;
  1593.             }
  1594.             ClipRightX = _MaxClipX;
  1595.           }
  1596.           
  1597.           for (x=ClipLeftX;x<ClipRightX;x++)
  1598.           {
  1599.              Buffer[x]=LookPalPhong[ TextureMap[ (u>>16)+( (v>>16)<<8) ] + PhongTBL[ (MiddleA>>16) ] ];
  1600.              u+=ScanU;
  1601.              v+=ScanV;
  1602.              MiddleA+=MiddleDa;
  1603.           }               
  1604.           LeftX   += LeftDx;
  1605.           RightX  += RightDx;
  1606.           LeftU   += LeftDu;
  1607.           LeftV   += LeftDv;
  1608.           RightU  += RightDu;
  1609.           RightV  += RightDv;
  1610.           LeftA   += LeftDa;   
  1611.           RightA  += RightDa;         
  1612.  
  1613.           ClipLeftX  = ( LeftX>>16 );
  1614.           ClipRightX = ( RightX>>16 );
  1615.           width      = 1+ClipRightX-ClipLeftX;
  1616.      
  1617.           ScanU    = (RightU-LeftU)/width;
  1618.           ScanV    = (RightV-LeftV)/width;            
  1619.           MiddleDa = (RightA-LeftA)/width;          
  1620.        }
  1621.     }
  1622.  
  1623.     if (!GENERAL)
  1624.       return;
  1625.  
  1626.     _X1 = _X2;                 // setup for FLAT_TOP.
  1627.     _Y1 = _Y2;
  1628.     _U1 = _U2;
  1629.     _V1 = _V2;
  1630.     _A1 = _A2;
  1631.  
  1632.     _X2 = _X3;
  1633.     _U2 = _U3;
  1634.     _V2 = _V3;
  1635.     _A2 = _A3;
  1636.  
  1637.     _X3 = old_X3;
  1638.     _Y3 = old_Y3;
  1639.     _U3 = old_U3;
  1640.     _V3 = old_V3;
  1641.     _A3 = old_A3;
  1642.     
  1643.     Buffer=_RendBuffer;     // save starting point of Buffer.
  1644.     
  1645.     FLAT_TOP: 
  1646.  
  1647.     if (_X2<_X1)
  1648.     {
  1649.       tempx=_X2; _X2=_X1; _X1=tempx;
  1650.       tempu=_U2; _U2=_U1; _U1=tempu;
  1651.       tempv=_V2; _V2=_V1; _V1=tempv;
  1652.       tempa=_A2; _A2=_A1; _A1=tempa;
  1653.     }
  1654.  
  1655.     height    = 65536/(_Y3-_Y1);
  1656.  
  1657.     LeftDx    = (_X3-_X1)*height;    // Inverse left and right slope.
  1658.     RightDx   = (_X3-_X2)*height;
  1659.     LeftDu    = (_U3-_U1)*height;
  1660.     LeftDv    = (_V3-_V1)*height;
  1661.     RightDu   = (_U3-_U2)*height;
  1662.     RightDv   = (_V3-_V2)*height;
  1663.     LeftDa    = (_A3-_A1)*height;  // compute intensity deltas.
  1664.     RightDa   = (_A3-_A2)*height; 
  1665.  
  1666.     LeftX     = _X1<<16;
  1667.     RightX    = (_X2<<16)+32768;
  1668.     LeftU     = _U1<<16;
  1669.     LeftV     = _V1<<16;
  1670.     RightU    = _U2<<16;
  1671.     RightV    = _V2<<16;
  1672.     LeftA     = _A1<<16;              // assign intensity to left and right side.
  1673.     RightA    = _A2<<16;
  1674.  
  1675.     ClipLeftX  = ( LeftX>>16 );
  1676.     ClipRightX = ( RightX>>16 );
  1677.     width      = 1+ClipRightX-ClipLeftX;
  1678.      
  1679.     ScanU    = (RightU-LeftU)/width;
  1680.     ScanV    = (RightV-LeftV)/width;            
  1681.     MiddleDa = (RightA-LeftA)/width;          
  1682.                        
  1683.     if (_Y1 < _MinClipY)
  1684.     {
  1685.        ydiff    = (_MinClipY - _Y1);
  1686.        LeftX    = LeftX+LeftDx*ydiff;
  1687.        RightX   = RightX+RightDx*ydiff;
  1688.  
  1689.        LeftU    = LeftU+LeftDu*ydiff;
  1690.        RightU   = RightU+RightDu*ydiff;
  1691.        LeftV    = LeftV+LeftDv*ydiff;
  1692.        RightV   = RightV+RightDv*ydiff;
  1693.  
  1694.        LeftA    = LeftA+LeftDa*ydiff;
  1695.        RightA   = RightA+RightDa*ydiff;
  1696.  
  1697.        ClipLeftX  = ( LeftX>>16 );
  1698.        ClipRightX = ( RightX>>16 );
  1699.        width      = 1+ClipRightX-ClipLeftX;
  1700.      
  1701.        ScanU    = (RightU-LeftU)/width;
  1702.        ScanV    = (RightV-LeftV)/width;            
  1703.        MiddleDa = (RightA-LeftA)/width;          
  1704.                        
  1705.              _Y1 = _MinClipY;
  1706.     }
  1707.                 
  1708.     if (_Y3 > _MaxClipY)
  1709.       _Y3 = _MaxClipY;
  1710.               
  1711.     Buffer+=(_Y1<<8)+(_Y1<<6);
  1712.  
  1713.     if (_X1>=_MinClipX && _X1<=_MaxClipX &&
  1714.         _X2>=_MinClipX && _X2<=_MaxClipX &&
  1715.         _X3>=_MinClipX && _X3<=_MaxClipX)
  1716.     {                   
  1717.        for (y=_Y1;y<_Y3;y++,Buffer+=SCREENWIDTH)    
  1718.        {        
  1719.           u=LeftU;
  1720.           v=LeftV;
  1721.           MiddleA=LeftA;
  1722.           for (x=( ClipLeftX );x<=( ClipRightX );x++)
  1723.           {                 
  1724.              Buffer[x]=LookPalPhong[ TextureMap[ (u>>16)+( (v>>16)<<8) ] + PhongTBL[ (MiddleA>>16) ] ];
  1725.              u+=ScanU;
  1726.              v+=ScanV;
  1727.              MiddleA+=MiddleDa;
  1728.           }
  1729.           LeftX   += LeftDx;
  1730.           RightX  += RightDx;
  1731.           LeftU   += LeftDu;
  1732.           LeftV   += LeftDv;
  1733.           RightU  += RightDu;
  1734.           RightV  += RightDv;
  1735.           LeftA   += LeftDa;      // update the intensities and slopes.
  1736.           RightA  += RightDa;
  1737.  
  1738.           ClipLeftX  = ( LeftX>>16 );
  1739.           ClipRightX = ( RightX>>16 );
  1740.           width      = 1+ClipRightX-ClipLeftX;
  1741.  
  1742.           ScanU    = (RightU-LeftU)/width;
  1743.           ScanV    = (RightV-LeftV)/width;            
  1744.           MiddleDa = (RightA-LeftA)/width;          
  1745.        }
  1746.     }
  1747.     else
  1748.     {
  1749.        for (y=_Y1;y<_Y3;y++,Buffer+=SCREENWIDTH)
  1750.        {
  1751.            
  1752.           u=LeftU;
  1753.           v=LeftV;
  1754.           MiddleA=LeftA;
  1755.  
  1756.           if (ClipLeftX < _MinClipX)
  1757.           {
  1758.             if (ClipRightX <= _MinClipX)
  1759.             {
  1760.                LeftX  += LeftDx;      // update the intensities and slopes.
  1761.                RightX += RightDx;     // before continuing.
  1762.                LeftU  += LeftDu;
  1763.                LeftV  += LeftDv;
  1764.                RightU += RightDu;
  1765.                RightV += RightDv;
  1766.                LeftA  += LeftDa;      
  1767.                RightA += RightDa;         
  1768.  
  1769.                ClipLeftX  = ( LeftX>>16 );
  1770.                ClipRightX = ( RightX>>16 );
  1771.  
  1772.                continue;
  1773.             }
  1774.             u          = LeftU + (_MinClipX - ClipLeftX)*ScanU;
  1775.             v          = LeftV + (_MinClipX - ClipLeftX)*ScanV;
  1776.             MiddleA    = LeftA + (_MinClipX - ClipLeftX)*MiddleDa;   // move the intensity by
  1777.             ClipLeftX  = _MinClipX;   // the clipped amount.
  1778.           }
  1779.  
  1780.           if (ClipRightX > _MaxClipX)
  1781.           {
  1782.             if (ClipLeftX > _MaxClipX)
  1783.             {
  1784.                LeftX  += LeftDx;      // update the intensities and slopes.
  1785.                RightX += RightDx;     // before continuing.
  1786.                LeftU  += LeftDu;
  1787.                LeftV  += LeftDv;
  1788.                RightU += RightDu;
  1789.                RightV += RightDv;
  1790.                LeftA  += LeftDa;      
  1791.                RightA += RightDa;
  1792.  
  1793.                ClipLeftX  = ( LeftX>>16 );
  1794.                ClipRightX = ( RightX>>16 );
  1795.  
  1796.                continue;
  1797.             }
  1798.             ClipRightX = _MaxClipX;
  1799.           }
  1800.                                             
  1801.           for (x=ClipLeftX;x<ClipRightX;x++)
  1802.           {
  1803.              Buffer[x]=LookPalPhong[ TextureMap[ (u>>16)+( (v>>16)<<8) ] + PhongTBL[ (MiddleA>>16) ] ];
  1804.              u+=ScanU;
  1805.              v+=ScanV;
  1806.              MiddleA+=MiddleDa;
  1807.           }               
  1808.           LeftX   += LeftDx;
  1809.           RightX  += RightDx;
  1810.           LeftU   += LeftDu;
  1811.           LeftV   += LeftDv;
  1812.           RightU  += RightDu;
  1813.           RightV  += RightDv;
  1814.           LeftA   += LeftDa;     
  1815.           RightA  += RightDa;         
  1816.  
  1817.           ClipLeftX  = ( LeftX>>16 );
  1818.           ClipRightX = ( RightX>>16 );
  1819.           width      = 1+ClipRightX-ClipLeftX;
  1820.  
  1821.           ScanU    = (RightU-LeftU)/width;
  1822.           ScanV    = (RightV-LeftV)/width;            
  1823.           MiddleDa = (RightA-LeftA)/width;          
  1824.        }
  1825.     }
  1826. }
  1827.  
  1828. void Scan_Convert_LambertT(void)
  1829. {
  1830.   long LeftX, RightX, LeftDx, RightDx;
  1831.   long slope, height;
  1832.   int x,y,ydiff,newx,tempx,tempy,old_X3,old_Y3,ClipLeftX,ClipRightX;
  1833.   int GENERAL;
  1834.   int fgcolor;
  1835.   unsigned char *Buffer;
  1836.   
  1837.     if ( (_X1==_X2 && _X2==_X3) || (_Y1==_Y2 && _Y2==_Y3) )
  1838.       return;               // degenerated into lines.
  1839.  
  1840.     if (_Y2<_Y1)              // switch. They're in the wrong order.
  1841.     {
  1842.         tempx=_X1; tempy=_Y1;
  1843.         _X1=_X2;    _Y1=_Y2;
  1844.         _X2=tempx; _Y2=tempy;
  1845.     }    
  1846.     if (_Y3<_Y1)              // switch. They're in the wrong order.
  1847.     {
  1848.         tempx=_X1; tempy=_Y1;
  1849.         _X1=_X3;    _Y1=_Y3;
  1850.         _X3=tempx; _Y3=tempy;
  1851.     }
  1852.     if (_Y3<_Y2)              // switch. They're in the wrong order.
  1853.     {
  1854.         tempx=_X2; tempy=_Y2;
  1855.         _X2=_X3;    _Y2=_Y3;
  1856.         _X3=tempx; _Y3=tempy;
  1857.     }
  1858.     
  1859.     if (_Y3<_MinClipY || _Y1>_MaxClipY ||
  1860.        (_X1<_MinClipX && _X2<_MinClipX && _X3<_MinClipX) ||
  1861.        (_X1>_MaxClipX && _X2>_MaxClipX && _X3>_MaxClipX) )
  1862.        return;              // do trivial rejection.
  1863.         
  1864.                             // now ALL vertices are in correct Counter-Clockwise order.
  1865.  
  1866.     GENERAL=FALSE;          // reset cases (for Triangle).
  1867.     Buffer=_RendBuffer;     // save starting point of Buffer.
  1868.       
  1869.     fgcolor=( _TransLevel<<16) + (LookPal[ _ColorIndex ]<<8); // TransTbl[ translevel*65536 + fg*256 + b]. 
  1870.            
  1871.     if (_Y1==_Y2)
  1872.       goto FLAT_TOP;
  1873.     if (_Y2==_Y3)
  1874.       goto FLAT_BOTTOM;
  1875.     else
  1876.       GENERAL=TRUE;
  1877.          
  1878.     slope = ((_X3-_X1)<<16)/(_Y3-_Y1);               
  1879.     newx  = _X1+( (slope*(_Y2-_Y1))>>16 );
  1880.     old_X3 = _X3;                            // save values for later.
  1881.     old_Y3 = _Y3;
  1882.  
  1883.     _X3    = newx;
  1884.     _Y3    = _Y2;           
  1885.  
  1886.     FLAT_BOTTOM:
  1887.         
  1888.     if (_X3<_X2)
  1889.     {
  1890.       tempx=_X3; _X3=_X2; _X2=tempx;
  1891.     }
  1892.  
  1893.     height  = 65536/(_Y3-_Y1);
  1894.     LeftDx  = (_X2-_X1)*height;    // Inverse left and right slope.
  1895.     RightDx = (_X3-_X1)*height;
  1896.  
  1897.     LeftX   = _X1<<16;
  1898.     RightX  = LeftX+32768;
  1899.  
  1900.     if (_Y1 < _MinClipY)
  1901.     {
  1902.         ydiff = (_MinClipY-_Y1);
  1903.        LeftX  = LeftX+LeftDx*ydiff;
  1904.        RightX = RightX+RightDx*ydiff;
  1905.            _Y1 = _MinClipY;
  1906.     }
  1907.                 
  1908.     if (_Y3 > _MaxClipY)
  1909.       _Y3 = _MaxClipY;
  1910.               
  1911.     Buffer+=(_Y1<<8)+(_Y1<<6);
  1912.  
  1913.     if (_X1>=_MinClipX && _X1<=_MaxClipX &&
  1914.         _X2>=_MinClipX && _X2<=_MaxClipX &&
  1915.         _X3>=_MinClipX && _X3<=_MaxClipX)
  1916.     {                   
  1917.        for (y=_Y1;y<_Y3;y++,Buffer+=SCREENWIDTH)    
  1918.        {
  1919.           for (x=( LeftX>>16 );x<=( RightX>>16 );x++)
  1920.           {
  1921.              Buffer[x]=TransTBL[ fgcolor + Buffer[x] ];
  1922.           }
  1923.           LeftX  += LeftDx;
  1924.           RightX += RightDx;
  1925.        }
  1926.     }
  1927.     else
  1928.     {
  1929.        for (y=_Y1;y<_Y3;y++,Buffer+=SCREENWIDTH)    
  1930.        {
  1931.  
  1932.           ClipLeftX  = ( LeftX>>16 );
  1933.           ClipRightX = ( RightX>>16 );
  1934.  
  1935.           if (ClipLeftX < _MinClipX)
  1936.           {
  1937.             if (ClipRightX <= _MinClipX)
  1938.             {
  1939.                LeftX   += LeftDx;      // update the intensities and slopes.
  1940.                RightX  += RightDx;     // before continuing.
  1941.                continue;
  1942.             }
  1943.             ClipLeftX = _MinClipX;                                // the clipped amount.
  1944.           }
  1945.  
  1946.           if (ClipRightX > _MaxClipX)
  1947.           {
  1948.             if (ClipLeftX > _MaxClipX)
  1949.             {
  1950.                LeftX   += LeftDx;      // update the intensities and slopes.
  1951.                RightX  += RightDx;     // before continuing.
  1952.                continue;
  1953.             }
  1954.             ClipRightX = _MaxClipX;
  1955.           }
  1956.  
  1957.           for (x=( ClipLeftX );x<=( ClipRightX );x++)
  1958.           {
  1959.              Buffer[x]=TransTBL[ fgcolor + Buffer[x] ];
  1960.           }
  1961.           LeftX  += LeftDx;
  1962.           RightX += RightDx;
  1963.        }
  1964.     }       
  1965.  
  1966.     if (!GENERAL)
  1967.       return;
  1968.  
  1969.     _X1     = _X2;                 // setup for FLAT_TOP.
  1970.     _Y1     = _Y2;
  1971.  
  1972.     _X2     = _X3;
  1973.  
  1974.     _X3     = old_X3;
  1975.     _Y3     = old_Y3;
  1976.  
  1977.     Buffer=_RendBuffer;     // save starting point of Buffer.
  1978.     
  1979.     FLAT_TOP: 
  1980.  
  1981.     if (_X2<_X1)
  1982.     {
  1983.       tempx=_X1; _X1=_X2; _X2=tempx;
  1984.     }
  1985.         
  1986.     height  = 65536/(_Y3-_Y1);
  1987.     LeftDx  = (_X3-_X1)*height;    // Inverse left and right slope.
  1988.     RightDx = (_X3-_X2)*height;
  1989.             
  1990.     LeftX   = _X1<<16;
  1991.     RightX  = (_X2<<16)+32768;
  1992.  
  1993.     if (_Y1 < _MinClipY)
  1994.     {
  1995.        ydiff = (_MinClipY-_Y1); 
  1996.       LeftX  = LeftX+LeftDx*ydiff;
  1997.       RightX = RightX+RightDx*ydiff;
  1998.           _Y1 = _MinClipY;
  1999.     }
  2000.                         
  2001.     if (_Y3 > _MaxClipY)
  2002.         _Y3 = _MaxClipY;
  2003.            
  2004.     Buffer+=(_Y1<<8)+(_Y1<<6);
  2005.  
  2006.     if (_X1>=_MinClipX && _X1<=_MaxClipX &&
  2007.         _X2>=_MinClipX && _X2<=_MaxClipX &&
  2008.         _X3>=_MinClipX && _X3<=_MaxClipX)
  2009.     {                   
  2010.        for (y=_Y1;y<_Y3;y++,Buffer+=SCREENWIDTH)
  2011.        {
  2012.           for (x=( LeftX>>16 );x<=( RightX>>16 );x++)
  2013.           {
  2014.              Buffer[x]=TransTBL[ fgcolor + Buffer[x] ];
  2015.           }
  2016.           LeftX  += LeftDx;
  2017.           RightX += RightDx;
  2018.        }
  2019.     }
  2020.     else
  2021.     {
  2022.        for (y=_Y1;y<_Y3;y++,Buffer+=SCREENWIDTH)
  2023.        {
  2024.           ClipLeftX  = ( LeftX>>16 );
  2025.           ClipRightX = ( RightX>>16 );
  2026.               
  2027.           if (ClipLeftX < _MinClipX)
  2028.           {
  2029.             if (ClipRightX <= _MinClipX)
  2030.             {
  2031.                LeftX   += LeftDx;      // update the intensities and slopes.
  2032.                RightX  += RightDx;     // before continuing.
  2033.                continue;
  2034.             }
  2035.             ClipLeftX = _MinClipX;                                // the clipped amount.
  2036.           }
  2037.  
  2038.           if (ClipRightX > _MaxClipX)
  2039.           {
  2040.             if (ClipLeftX > _MaxClipX)
  2041.             {
  2042.                LeftX   += LeftDx;      // update the intensities and slopes.
  2043.                RightX  += RightDx;     // before continuing.
  2044.                continue;
  2045.             }
  2046.             ClipRightX = _MaxClipX;
  2047.           }
  2048.                                             
  2049.           for (x=( ClipLeftX );x<=( ClipRightX );x++)
  2050.           {
  2051.              Buffer[x]=TransTBL[ fgcolor + Buffer[x] ];
  2052.           }
  2053.           LeftX  += LeftDx;
  2054.           RightX += RightDx;
  2055.        }
  2056.     }
  2057. }
  2058.  
  2059. void Scan_Convert_GouraudT(void)
  2060. {
  2061.   long LeftX, RightX, LeftDx, RightDx;
  2062.   long LeftI,RightI,MiddleI;
  2063.   long LeftDi,RightDi,MiddleDi;
  2064.   long slope,height;
  2065.   int x,y,ydiff,newx,tempx,tempy,tempi;
  2066.   int old_X3,old_Y3,old_I3,newi,ClipLeftX,ClipRightX;
  2067.   int GENERAL;
  2068.   unsigned char *Buffer;
  2069.   unsigned char *TransTBLptr;
  2070.   
  2071.     if ( (_X1==_X2 && _X2==_X3) || (_Y1==_Y2 && _Y2==_Y3) )
  2072.       return;               // degenerated into lines.       
  2073.     
  2074.     if (_Y2<_Y1)              // switch. They're in the wrong order.
  2075.     {
  2076.         tempx=_X1; tempy=_Y1; tempi=_I1;
  2077.         _X1=_X2;    _Y1=_Y2;    _I1=_I2;
  2078.         _X2=tempx; _Y2=tempy; _I2=tempi;
  2079.     }    
  2080.     if (_Y3<_Y1)              // switch. They're in the wrong order.
  2081.     {
  2082.         tempx=_X1; tempy=_Y1; tempi=_I1;
  2083.         _X1=_X3;    _Y1=_Y3;    _I1=_I3;
  2084.         _X3=tempx; _Y3=tempy; _I3=tempi;
  2085.     }
  2086.     if (_Y3<_Y2)              // switch. They're in the wrong order.
  2087.     {
  2088.         tempx=_X2; tempy=_Y2; tempi=_I2;
  2089.         _X2=_X3;    _Y2=_Y3;    _I2=_I3;
  2090.         _X3=tempx; _Y3=tempy; _I3=tempi;
  2091.     }
  2092.  
  2093.     if (_Y3<_MinClipY || _Y1>_MaxClipY ||
  2094.        (_X1<_MinClipX && _X2<_MinClipX && _X3<_MinClipX) ||
  2095.        (_X1>_MaxClipX && _X2>_MaxClipX && _X3>_MaxClipX) )
  2096.        return;              // do trivial rejection.
  2097.         
  2098.                             // now ALL vertices are in correct Counter-Clockwise order.
  2099.  
  2100.     GENERAL=FALSE;          // reset cases (for Triangle).
  2101.     Buffer=_RendBuffer;     // save starting point of Buffer.
  2102.  
  2103.     TransTBLptr = TransTBL;
  2104.     TransTBLptr += ( _TransLevel<<16);    // Buffer at level of transparency.
  2105.         
  2106.     if (_Y1==_Y2)
  2107.       goto FLAT_TOP;
  2108.     if (_Y2==_Y3)
  2109.       goto FLAT_BOTTOM;
  2110.     else
  2111.       GENERAL=TRUE;
  2112.          
  2113.     height  = 65536/(_Y3-_Y1);
  2114.     slope   = (_X3-_X1)*height;               
  2115.     newx    = _X1+( (slope*(_Y2-_Y1))>>16 );
  2116.     newi    = ( (((_Y2-_Y1)*_I3+(_Y3-_Y2)*_I1)*height)>>16 );
  2117.               
  2118.     old_X3   = _X3;                            // save values for later.
  2119.     old_Y3   = _Y3;
  2120.     old_I3   = _I3;                        // save int3 for bottom half.
  2121.  
  2122.     _X3      = newx;
  2123.     _Y3      = _Y2;           
  2124.     _I3      = newi;
  2125.        
  2126.     FLAT_BOTTOM:
  2127.  
  2128.     if (_X3<_X2)
  2129.     {
  2130.       tempx=_X3; _X3=_X2; _X2=tempx;
  2131.       tempi=_I3; _I3=_I2; _I2=tempi;
  2132.     }
  2133.  
  2134.     height   = 65536/(_Y3-_Y1);
  2135.     LeftDx   = (_X2-_X1)*height;    // Inverse left and right slope.
  2136.     RightDx  = (_X3-_X1)*height;
  2137.  
  2138.     LeftX    = _X1<<16;              // assign Buffering coords.
  2139.     RightX   = LeftX+32768;
  2140.  
  2141.     LeftDi   = (_I2 - _I1)*height;  // compute intensity deltas.
  2142.     RightDi  = (_I3 - _I1)*height; 
  2143.     MiddleDi = 0;
  2144.  
  2145.     LeftI    = _I1<<16;              // assign intensity to left and right side.
  2146.     RightI   = LeftI;
  2147.     MiddleI  = LeftI;        // Buffering from left intensity.
  2148.  
  2149.     ClipLeftX  = ( LeftX>>16 );
  2150.     ClipRightX = ( RightX>>16 );
  2151.  
  2152.     if (_Y1 < _MinClipY)
  2153.     {
  2154.        ydiff    = (_MinClipY - _Y1);   
  2155.        LeftX    = LeftX+LeftDx*ydiff;
  2156.        RightX   = RightX+RightDx*ydiff;
  2157.  
  2158.        LeftI    = LeftI+LeftDi*ydiff;
  2159.        RightI   = RightI+RightDi*ydiff;
  2160.        MiddleI  = LeftI;
  2161.  
  2162.        ClipLeftX  = ( LeftX>>16 );
  2163.        ClipRightX = ( RightX>>16 );
  2164.  
  2165.        MiddleDi = (RightI - LeftI)/(1+ClipRightX-ClipLeftX);  // to avoid divide by 0.
  2166.        
  2167.            _Y1   = _MinClipY;
  2168.     }
  2169.                 
  2170.     if (_Y3 > _MaxClipY)
  2171.       _Y3 = _MaxClipY;
  2172.               
  2173.     Buffer+=(_Y1<<8)+(_Y1<<6);
  2174.  
  2175.     if (_X1>=_MinClipX && _X1<=_MaxClipX &&
  2176.         _X2>=_MinClipX && _X2<=_MaxClipX &&
  2177.         _X3>=_MinClipX && _X3<=_MaxClipX)
  2178.     {                   
  2179.        for (y=_Y1;y<_Y3;y++,Buffer+=SCREENWIDTH)
  2180.        {
  2181.           for (x=( ClipLeftX );x<=( ClipRightX );x++)
  2182.           {
  2183.              Buffer[x]=TransTBLptr[ ( LookPal[ _ColorIndex + (MiddleI>>16) ]<<8 ) + Buffer[x] ];
  2184.              MiddleI+=MiddleDi;
  2185.           }
  2186.         LeftI   += LeftDi;      // update the intensities and slopes.
  2187.         RightI  += RightDi;
  2188.         MiddleI  = LeftI;      
  2189.         LeftX   += LeftDx;
  2190.         RightX  += RightDx;
  2191.  
  2192.         ClipLeftX  = ( LeftX>>16 );
  2193.         ClipRightX = ( RightX>>16 );
  2194.     
  2195.         MiddleDi = (RightI - LeftI)/( 1+ClipRightX-ClipLeftX );  // to avoid divide by 0.
  2196.        }
  2197.     }
  2198.     else
  2199.     {
  2200.        for (y=_Y1;y<_Y3;y++,Buffer+=SCREENWIDTH)
  2201.        {
  2202.  
  2203.           if (ClipLeftX < _MinClipX)
  2204.           {
  2205.             if (ClipRightX <= _MinClipX)
  2206.             {
  2207.                LeftX   += LeftDx;      // update the intensities and slopes.
  2208.                RightX  += RightDx;     // before continuing.
  2209.                LeftI   += LeftDi;      
  2210.                RightI  += RightDi;         
  2211.  
  2212.                ClipLeftX  = ( LeftX>>16 );
  2213.                ClipRightX = ( RightX>>16 );
  2214.  
  2215.                continue;
  2216.             }
  2217.             MiddleI = LeftI + (_MinClipX - ClipLeftX)*MiddleDi;   // move the intensity by  
  2218.             ClipLeftX = _MinClipX;                                // the clipped amount.
  2219.           }
  2220.  
  2221.           if (ClipRightX > _MaxClipX)
  2222.           {
  2223.             if (ClipLeftX > _MaxClipX)
  2224.             {
  2225.                LeftX   += LeftDx;      // update the intensities and slopes.
  2226.                RightX  += RightDx;     // before continuing.
  2227.                LeftI   += LeftDi;
  2228.                RightI  += RightDi;
  2229.  
  2230.                ClipLeftX  = ( LeftX>>16 );
  2231.                ClipRightX = ( RightX>>16 );
  2232.  
  2233.                continue;
  2234.             }
  2235.             ClipRightX = _MaxClipX;
  2236.           }
  2237.                       
  2238.           for (x=ClipLeftX;x<=ClipRightX;x++)
  2239.           {
  2240.              Buffer[x]=TransTBLptr[ ( LookPal[ _ColorIndex + (MiddleI>>16) ]<<8 ) + Buffer[x] ];
  2241.              MiddleI  += MiddleDi;
  2242.           }
  2243.           LeftX   += LeftDx;
  2244.           RightX  += RightDx;
  2245.           LeftI   += LeftDi;   
  2246.           RightI  += RightDi;         
  2247.           MiddleI  = LeftI;      
  2248.  
  2249.           ClipLeftX  = ( LeftX>>16 );
  2250.           ClipRightX = ( RightX>>16 );
  2251.  
  2252.           MiddleDi = (RightI - LeftI)/( 1+ClipRightX-ClipLeftX );  // to avoid divide by 0.
  2253.        }
  2254.     }
  2255.  
  2256.     if (!GENERAL)
  2257.       return;
  2258.  
  2259.     _X1     = _X2;                 // setup for FLAT_TOP.
  2260.     _Y1     = _Y2;
  2261.     _I1     = _I2;
  2262.  
  2263.     _X2     = _X3;
  2264.     _I2     = _I3;
  2265.  
  2266.     _X3     = old_X3;
  2267.     _Y3     = old_Y3;
  2268.     _I3     = old_I3;
  2269.     
  2270.     Buffer=_RendBuffer;     // save starting point of Buffer.
  2271.     
  2272.     FLAT_TOP: 
  2273.  
  2274.     if (_X2<_X1)
  2275.     {
  2276.       tempx=_X2; _X2=_X1; _X1=tempx;
  2277.       tempi=_I2; _I2=_I1; _I1=tempi;
  2278.     }
  2279.  
  2280.     height   = 65536/(_Y3-_Y1);
  2281.     LeftDx   = (_X3-_X1)*height;    // Inverse left and right slope.
  2282.     RightDx  = (_X3-_X2)*height;
  2283.  
  2284.     LeftX    = _X1<<16;              // assign Buffering coords.
  2285.     RightX   = (_X2<<16)+32768;
  2286.  
  2287.     LeftI    = _I1<<16;              // assign intensity to left and right side.
  2288.     RightI   = _I2<<16;
  2289.     MiddleI  = LeftI;        // Buffering from left intensity.
  2290.  
  2291.     LeftDi   = (_I3 - _I1)*height;  // compute intensity deltas.
  2292.     RightDi  = (_I3 - _I2)*height; 
  2293.  
  2294.     ClipLeftX  = ( LeftX>>16 );
  2295.     ClipRightX = ( RightX>>16 );
  2296.  
  2297.     MiddleDi = (RightI - LeftI)/( 1+ClipRightX-ClipLeftX );  // to avoid divide by 0.
  2298.                
  2299.     if (_Y1 < _MinClipY)
  2300.     {
  2301.         ydiff   = (_MinClipY - _Y1);
  2302.         LeftX   = LeftX+LeftDx*ydiff;
  2303.         RightX  = RightX+RightDx*ydiff;
  2304.  
  2305.        LeftI    = LeftI+LeftDi*ydiff;
  2306.        RightI   = RightI+RightDi*ydiff;
  2307.        MiddleI  = LeftI;
  2308.        
  2309.        ClipLeftX  = ( LeftX>>16 );
  2310.        ClipRightX = ( RightX>>16 );
  2311.  
  2312.        MiddleDi = (RightI - LeftI)/( 1+ClipRightX-ClipLeftX );  // to avoid divide by 0.       
  2313.  
  2314.             _Y1  = _MinClipY;
  2315.     }
  2316.                 
  2317.     if (_Y3 > _MaxClipY)
  2318.       _Y3 = _MaxClipY;
  2319.               
  2320.     Buffer+=(_Y1<<8)+(_Y1<<6);
  2321.  
  2322.     if (_X1>=_MinClipX && _X1<=_MaxClipX &&
  2323.         _X2>=_MinClipX && _X2<=_MaxClipX &&
  2324.         _X3>=_MinClipX && _X3<=_MaxClipX)
  2325.     {                   
  2326.        for (y=_Y1;y<_Y3;y++,Buffer+=SCREENWIDTH)    
  2327.        {
  2328.           for (x=( ClipLeftX );x<=( ClipRightX );x++)
  2329.           {
  2330.              Buffer[x]=TransTBLptr[ ( LookPal[ _ColorIndex + (MiddleI>>16) ]<<8 ) + Buffer[x] ];
  2331.              MiddleI  += MiddleDi;
  2332.           }               
  2333.         LeftI   += LeftDi;      // update the intensities and slopes.
  2334.         RightI  += RightDi;
  2335.         MiddleI  = LeftI;      
  2336.         LeftX   += LeftDx;
  2337.         RightX  += RightDx;
  2338.  
  2339.         ClipLeftX  = ( LeftX>>16 );
  2340.         ClipRightX = ( RightX>>16 );
  2341.  
  2342.         MiddleDi = (RightI - LeftI)/( 1+ClipRightX-ClipLeftX );  // to avoid divide by 0.
  2343.        }
  2344.     }
  2345.     else
  2346.     {
  2347.        for (y=_Y1;y<_Y3;y++,Buffer+=SCREENWIDTH)    
  2348.        {
  2349.  
  2350.           if (ClipLeftX < _MinClipX)
  2351.           {
  2352.             if (ClipRightX <= _MinClipX)
  2353.             {
  2354.                LeftX   += LeftDx;      // update the intensities and slopes.
  2355.                RightX  += RightDx;     // before continuing.
  2356.                LeftI   += LeftDi;      
  2357.                RightI  += RightDi;         
  2358.  
  2359.                ClipLeftX  = ( LeftX>>16 );
  2360.                ClipRightX = ( RightX>>16 );
  2361.  
  2362.                continue;
  2363.             }
  2364.             MiddleI = LeftI + (_MinClipX - ClipLeftX)*MiddleDi;   // move the intensity by  
  2365.             ClipLeftX = _MinClipX;                                // the clipped amount.
  2366.           }
  2367.  
  2368.           if (ClipRightX > _MaxClipX)
  2369.           {
  2370.             if (ClipLeftX > _MaxClipX)
  2371.             {
  2372.                LeftX   += LeftDx;      // update the intensities and slopes.
  2373.                RightX  += RightDx;     // before continuing.
  2374.                LeftI   += LeftDi;      
  2375.                RightI  += RightDi;         
  2376.  
  2377.                ClipLeftX  = ( LeftX>>16 );
  2378.                ClipRightX = ( RightX>>16 );
  2379.  
  2380.                continue;
  2381.             }
  2382.             ClipRightX = _MaxClipX;
  2383.           }
  2384.                                             
  2385.           for (x=ClipLeftX;x<=ClipRightX;x++)
  2386.           {
  2387.              Buffer[x]=TransTBLptr[ ( LookPal[ _ColorIndex + (MiddleI>>16) ]<<8 ) + Buffer[x] ];
  2388.              MiddleI  += MiddleDi;
  2389.           }               
  2390.           LeftX   += LeftDx;
  2391.           RightX  += RightDx;
  2392.           LeftI   += LeftDi;     
  2393.           RightI  += RightDi;         
  2394.           MiddleI  = LeftI;      
  2395.  
  2396.           ClipLeftX     = ( LeftX>>16 );
  2397.           ClipRightX    = ( RightX>>16 );
  2398.  
  2399.           MiddleDi = (RightI - LeftI)/( 1+ClipRightX-ClipLeftX );  // to avoid divide by 0.
  2400.        }
  2401.     }           
  2402. }
  2403.  
  2404. void Scan_Convert_PhongT(void)
  2405. {
  2406.   long LeftX, RightX, LeftDx, RightDx;
  2407.   long LeftA,RightA,MiddleA;
  2408.   long LeftDa,RightDa,MiddleDa;
  2409.   long slope,height;
  2410.   int x,y,ydiff,newx,tempx,tempy,tempa;
  2411.   int old_X3,old_Y3,old_A3,newa,ClipLeftX,ClipRightX;
  2412.   int GENERAL;
  2413.   unsigned char *Buffer;
  2414.   unsigned char *TransTBLptr;
  2415.   
  2416.     if ( (_X1==_X2 && _X2==_X3) || (_Y1==_Y2 && _Y2==_Y3) )
  2417.       return;               // degenerated into lines.       
  2418.     
  2419.     if (_Y2<_Y1)              // switch. They're in the wrong order.
  2420.     {
  2421.         tempx=_X1; tempy=_Y1; tempa=_A1;
  2422.         _X1=_X2;    _Y1=_Y2;    _A1=_A2;
  2423.         _X2=tempx; _Y2=tempy; _A2=tempa;
  2424.     }    
  2425.     if (_Y3<_Y1)              // switch. They're in the wrong order.
  2426.     {
  2427.         tempx=_X1; tempy=_Y1; tempa=_A1;
  2428.         _X1=_X3;    _Y1=_Y3;    _A1=_A3;
  2429.         _X3=tempx; _Y3=tempy; _A3=tempa;
  2430.     }
  2431.     if (_Y3<_Y2)              // switch. They're in the wrong order.
  2432.     {
  2433.         tempx=_X2; tempy=_Y2; tempa=_A2;
  2434.         _X2=_X3;    _Y2=_Y3;    _A2=_A3;
  2435.         _X3=tempx; _Y3=tempy; _A3=tempa;
  2436.     }
  2437.  
  2438.     if (_Y3<_MinClipY || _Y1>_MaxClipY ||
  2439.        (_X1<_MinClipX && _X2<_MinClipX && _X3<_MinClipX) ||
  2440.        (_X1>_MaxClipX && _X2>_MaxClipX && _X3>_MaxClipX) )
  2441.        return;              // do trivial rejection.
  2442.         
  2443.                             // now ALL vertices are in correct Counter-Clockwise order.
  2444.  
  2445.     GENERAL=FALSE;          // reset cases (for Triangle).
  2446.     Buffer=_RendBuffer;     // save starting point of Buffer.
  2447.  
  2448.     TransTBLptr = TransTBL;
  2449.     TransTBLptr += ( _TransLevel<<16);    // Buffer at level of transparency.
  2450.         
  2451.     if (_Y1==_Y2)
  2452.       goto FLAT_TOP;
  2453.     if (_Y2==_Y3)
  2454.       goto FLAT_BOTTOM;
  2455.     else
  2456.       GENERAL=TRUE;
  2457.          
  2458.     height  = 65536/(_Y3-_Y1);
  2459.     slope   = (_X3-_X1)*height;               
  2460.     newx    = _X1+( (slope*(_Y2-_Y1))>>16 );
  2461.     newa    = ( (((_Y2-_Y1)*_A3+(_Y3-_Y2)*_A1)*height)>>16 );
  2462.               
  2463.     old_X3   = _X3;                            // save values for later.
  2464.     old_Y3   = _Y3;
  2465.     old_A3   = _A3;                        // save int3 for bottom half.
  2466.  
  2467.     _X3      = newx;
  2468.     _Y3      = _Y2;           
  2469.     _A3      = newa;
  2470.        
  2471.     FLAT_BOTTOM:
  2472.  
  2473.     if (_X3<_X2)
  2474.     {
  2475.       tempx=_X3; _X3=_X2; _X2=tempx;
  2476.       tempa=_A3; _A3=_A2; _A2=tempa;
  2477.     }
  2478.  
  2479.     height   = 65536/(_Y3-_Y1);
  2480.     LeftDx   = (_X2-_X1)*height;    // Inverse left and right slope.
  2481.     RightDx  = (_X3-_X1)*height;
  2482.  
  2483.     LeftX    = _X1<<16;              // assign Buffering coords.
  2484.     RightX   = LeftX+32768;
  2485.  
  2486.     LeftDa   = (_A2 - _A1)*height;  // compute intensity deltas.
  2487.     RightDa  = (_A3 - _A1)*height; 
  2488.     MiddleDa = 0;
  2489.  
  2490.     LeftA    = _A1<<16;              // assign intensity to left and right side.
  2491.     RightA   = LeftA;
  2492.     MiddleA  = LeftA;        // Buffering from left intensity.
  2493.  
  2494.     ClipLeftX  = ( LeftX>>16 );
  2495.     ClipRightX = ( RightX>>16 );
  2496.  
  2497.     if (_Y1 < _MinClipY)
  2498.     {
  2499.        ydiff    = (_MinClipY - _Y1);   
  2500.        LeftX    = LeftX+LeftDx*ydiff;
  2501.        RightX   = RightX+RightDx*ydiff;
  2502.  
  2503.        LeftA    = LeftA+LeftDa*ydiff;
  2504.        RightA   = RightA+RightDa*ydiff;
  2505.        MiddleA  = LeftA;
  2506.  
  2507.        ClipLeftX  = ( LeftX>>16 );
  2508.        ClipRightX = ( RightX>>16 );
  2509.  
  2510.        MiddleDa = (RightA - LeftA)/( 1+ClipRightX-ClipLeftX );  // to avoid divide by 0.
  2511.        
  2512.            _Y1   = _MinClipY;
  2513.     }
  2514.                 
  2515.     if (_Y3 > _MaxClipY)
  2516.       _Y3 = _MaxClipY;
  2517.               
  2518.     Buffer+=(_Y1<<8)+(_Y1<<6);
  2519.  
  2520.     if (_X1>=_MinClipX && _X1<=_MaxClipX &&
  2521.         _X2>=_MinClipX && _X2<=_MaxClipX &&
  2522.         _X3>=_MinClipX && _X3<=_MaxClipX)
  2523.     {                   
  2524.        for (y=_Y1;y<_Y3;y++,Buffer+=SCREENWIDTH)
  2525.        {
  2526.           for (x=( ClipLeftX );x<=( ClipRightX );x++)
  2527.           {
  2528.              Buffer[x]=TransTBLptr[ ( LookPalPhong[ _ColorIndex + PhongTBL[ (MiddleA>>16) ] ]<<8 ) + Buffer[x] ];
  2529.              MiddleA+=MiddleDa;
  2530.           }
  2531.         LeftA   += LeftDa;      // update the intensities and slopes.
  2532.         RightA  += RightDa;
  2533.         MiddleA  = LeftA;      
  2534.         LeftX   += LeftDx;
  2535.         RightX  += RightDx;
  2536.  
  2537.         ClipLeftX  = ( LeftX>>16 );
  2538.         ClipRightX = ( RightX>>16 );
  2539.  
  2540.         MiddleDa = (RightA - LeftA)/( 1+ClipRightX-ClipLeftX );  // to avoid divide by 0.
  2541.        }
  2542.     }
  2543.     else
  2544.     {
  2545.        for (y=_Y1;y<_Y3;y++,Buffer+=SCREENWIDTH)
  2546.        {
  2547.  
  2548.           if (ClipLeftX < _MinClipX)
  2549.           {
  2550.             if (ClipRightX <= _MinClipX)
  2551.             {
  2552.                LeftX   += LeftDx;      // update the intensities and slopes.
  2553.                RightX  += RightDx;     // before continuing.
  2554.                LeftA   += LeftDa;      
  2555.                RightA  += RightDa;         
  2556.  
  2557.                ClipLeftX  = ( LeftX>>16 );
  2558.                ClipRightX = ( RightX>>16 );
  2559.  
  2560.                continue;
  2561.             }
  2562.             MiddleA = LeftA + (_MinClipX - ClipLeftX)*MiddleDa;   // move the intensity by  
  2563.             ClipLeftX = _MinClipX;                                // the clipped amount.
  2564.           }
  2565.  
  2566.           if (ClipRightX > _MaxClipX)
  2567.           {
  2568.             if (ClipLeftX > _MaxClipX)
  2569.             {
  2570.                LeftX   += LeftDx;      // update the intensities and slopes.
  2571.                RightX  += RightDx;     // before continuing.
  2572.                LeftA   += LeftDa;      
  2573.                RightA  += RightDa;         
  2574.  
  2575.                ClipLeftX  = ( LeftX>>16 );
  2576.                ClipRightX = ( RightX>>16 );
  2577.  
  2578.                continue;
  2579.             }
  2580.             ClipRightX = _MaxClipX;
  2581.           }
  2582.                       
  2583.           for (x=ClipLeftX;x<=ClipRightX;x++)
  2584.           {
  2585.              Buffer[x]=TransTBLptr[ ( LookPalPhong[ _ColorIndex + PhongTBL[ (MiddleA>>16) ] ]<<8 ) + Buffer[x] ];
  2586.              MiddleA  += MiddleDa;
  2587.           }
  2588.           LeftX   += LeftDx;
  2589.           RightX  += RightDx;
  2590.           LeftA   += LeftDa;   
  2591.           RightA  += RightDa;         
  2592.           MiddleA  = LeftA;      
  2593.  
  2594.           ClipLeftX  = ( LeftX>>16 );
  2595.           ClipRightX = ( RightX>>16 );
  2596.  
  2597.           MiddleDa = (RightA - LeftA)/( 1+ClipRightX-ClipLeftX );  // to avoid divide by 0.
  2598.        }
  2599.     }
  2600.  
  2601.     if (!GENERAL)
  2602.       return;
  2603.  
  2604.     _X1     = _X2;                 // setup for FLAT_TOP.
  2605.     _Y1     = _Y2;
  2606.     _A1     = _A2;
  2607.  
  2608.     _X2     = _X3;
  2609.     _A2     = _A3;
  2610.  
  2611.     _X3     = old_X3;
  2612.     _Y3     = old_Y3;
  2613.     _A3     = old_A3;
  2614.     
  2615.     Buffer=_RendBuffer;     // save starting point of Buffer.
  2616.     
  2617.     FLAT_TOP: 
  2618.  
  2619.     if (_X2<_X1)
  2620.     {
  2621.       tempx=_X2; _X2=_X1; _X1=tempx;
  2622.       tempa=_A2; _A2=_A1; _A1=tempa;
  2623.     }
  2624.  
  2625.     height   = 65536/(_Y3-_Y1);
  2626.     LeftDx   = (_X3-_X1)*height;    // Inverse left and right slope.
  2627.     RightDx  = (_X3-_X2)*height;
  2628.  
  2629.     LeftX    = _X1<<16;              // assign Buffering coords.
  2630.     RightX   = (_X2<<16)+32768;
  2631.  
  2632.     LeftA    = _A1<<16;              // assign intensity to left and right side.
  2633.     RightA   = _A2<<16;
  2634.     MiddleA  = LeftA;        // Buffering from left intensity.
  2635.  
  2636.     LeftDa   = (_A3 - _A1)*height;  // compute intensity deltas.
  2637.     RightDa  = (_A3 - _A2)*height; 
  2638.  
  2639.     ClipLeftX     = ( LeftX>>16 );
  2640.     ClipRightX    = ( RightX>>16 );
  2641.  
  2642.     MiddleDa = (RightA - LeftA)/( 1+ClipRightX-ClipLeftX );  // to avoid divide by 0.
  2643.                
  2644.     if (_Y1 < _MinClipY)
  2645.     {
  2646.         ydiff   = (_MinClipY - _Y1);
  2647.         LeftX   = LeftX+LeftDx*ydiff;
  2648.         RightX  = RightX+RightDx*ydiff;
  2649.  
  2650.        LeftA    = LeftA+LeftDa*ydiff;
  2651.        RightA   = RightA+RightDa*ydiff;
  2652.        MiddleA  = LeftA;
  2653.  
  2654.        ClipLeftX     = ( LeftX>>16 );
  2655.        ClipRightX    = ( RightX>>16 );
  2656.  
  2657.        MiddleDa = (RightA - LeftA)/( 1+ClipRightX-ClipLeftX );  // to avoid divide by 0.       
  2658.  
  2659.             _Y1  = _MinClipY;
  2660.     }
  2661.                 
  2662.     if (_Y3 > _MaxClipY)
  2663.       _Y3 = _MaxClipY;
  2664.               
  2665.     Buffer+=(_Y1<<8)+(_Y1<<6);
  2666.  
  2667.     if (_X1>=_MinClipX && _X1<=_MaxClipX &&
  2668.         _X2>=_MinClipX && _X2<=_MaxClipX &&
  2669.         _X3>=_MinClipX && _X3<=_MaxClipX)
  2670.     {                   
  2671.        for (y=_Y1;y<_Y3;y++,Buffer+=SCREENWIDTH)    
  2672.        {
  2673.           for (x=( ClipLeftX );x<=( ClipRightX );x++)
  2674.           {
  2675.              Buffer[x]=TransTBLptr[ ( LookPalPhong[ _ColorIndex + PhongTBL[ (MiddleA>>16) ] ]<<8 ) + Buffer[x] ];
  2676.              MiddleA  += MiddleDa;
  2677.           }               
  2678.         LeftA   += LeftDa;      // update the intensities and slopes.
  2679.         RightA  += RightDa;
  2680.         MiddleA  = LeftA;      
  2681.         LeftX   += LeftDx;
  2682.         RightX  += RightDx;
  2683.  
  2684.         ClipLeftX     = ( LeftX>>16 );
  2685.         ClipRightX    = ( RightX>>16 );
  2686.  
  2687.         MiddleDa = (RightA - LeftA)/( 1+ClipRightX-ClipLeftX );  // to avoid divide by 0.
  2688.        }
  2689.     }
  2690.     else
  2691.     {
  2692.        for (y=_Y1;y<_Y3;y++,Buffer+=SCREENWIDTH)    
  2693.        {
  2694.  
  2695.           if (ClipLeftX < _MinClipX)
  2696.           {
  2697.             if (ClipRightX <= _MinClipX)
  2698.             {
  2699.                LeftX   += LeftDx;      // update the intensities and slopes.
  2700.                RightX  += RightDx;     // before continuing.
  2701.                LeftA   += LeftDa;      
  2702.                RightA  += RightDa;         
  2703.  
  2704.                ClipLeftX  = ( LeftX>>16 );
  2705.                ClipRightX = ( RightX>>16 );
  2706.  
  2707.                continue;
  2708.             }
  2709.             MiddleA = LeftA + (_MinClipX - ClipLeftX)*MiddleDa;   // move the intensity by  
  2710.             ClipLeftX = _MinClipX;                                // the clipped amount.
  2711.           }
  2712.  
  2713.           if (ClipRightX > _MaxClipX)
  2714.           {
  2715.             if (ClipLeftX > _MaxClipX)
  2716.             {
  2717.                LeftX   += LeftDx;      // update the intensities and slopes.
  2718.                RightX  += RightDx;     // before continuing.
  2719.                LeftA   += LeftDa;      
  2720.                RightA  += RightDa;         
  2721.  
  2722.                ClipLeftX  = ( LeftX>>16 );
  2723.                ClipRightX = ( RightX>>16 );
  2724.  
  2725.                continue;
  2726.             }
  2727.             ClipRightX = _MaxClipX;
  2728.           }
  2729.                                             
  2730.           for (x=ClipLeftX;x<=ClipRightX;x++)
  2731.           {
  2732.              Buffer[x]=TransTBLptr[ ( LookPalPhong[ _ColorIndex + PhongTBL[ (MiddleA>>16) ] ]<<8 ) + Buffer[x] ];
  2733.              MiddleA  += MiddleDa;
  2734.           }               
  2735.           LeftX   += LeftDx;
  2736.           RightX  += RightDx;
  2737.           LeftA   += LeftDa;     
  2738.           RightA  += RightDa;         
  2739.           MiddleA  = LeftA;      
  2740.  
  2741.           ClipLeftX     = ( LeftX>>16 );
  2742.           ClipRightX    = ( RightX>>16 );
  2743.  
  2744.           MiddleDa = (RightA - LeftA)/( 1+ClipRightX-ClipLeftX );  // to avoid divide by 0.
  2745.        }
  2746.     }           
  2747. }
  2748.  
  2749. void Scan_Convert_TextureLT(void)
  2750. {
  2751.   long LeftX, RightX, LeftDx, RightDx;
  2752.   long width,height,slope;
  2753.   long u,v,ScanU,ScanV,LeftU,LeftV,RightU,RightV;
  2754.   long LeftDu,LeftDv,RightDu,RightDv;
  2755.   int ClipLeftX,ClipRightX;
  2756.   int x,y,ydiff,newx,newu,newv,tempx,tempy,tempu,tempv;
  2757.   int old_X3,old_Y3,old_U3,old_V3;
  2758.   int GENERAL;
  2759.   unsigned char *Buffer;
  2760.   unsigned char *TransTBLptr;
  2761.     
  2762.     if ( (_X1==_X2 && _X2==_X3) || (_Y1==_Y2 && _Y2==_Y3) )
  2763.       return;               // degenerated into lines.       
  2764.  
  2765.     if (_Y2<_Y1)              // switch. They're in the wrong order.
  2766.     {
  2767.         tempx=_X1; tempy=_Y1;
  2768.         _X1=_X2;    _Y1=_Y2;
  2769.         _X2=tempx; _Y2=tempy;
  2770.  
  2771.         tempu=_U1; tempv=_V1;
  2772.         _U1=_U2;    _V1=_V2;
  2773.         _U2=tempu; _V2=tempv;
  2774.     }    
  2775.     if (_Y3<_Y1)              // switch. They're in the wrong order.
  2776.     {
  2777.         tempx=_X1; tempy=_Y1;
  2778.         _X1=_X3;    _Y1=_Y3;
  2779.         _X3=tempx; _Y3=tempy;
  2780.  
  2781.         tempu=_U1; tempv=_V1;
  2782.         _U1=_U3;    _V1=_V3;
  2783.         _U3=tempu; _V3=tempv;
  2784.     }
  2785.     if (_Y3<_Y2)              // switch. They're in the wrong order.
  2786.     {
  2787.         tempx=_X2; tempy=_Y2;
  2788.         _X2=_X3;    _Y2=_Y3;
  2789.         _X3=tempx; _Y3=tempy;
  2790.  
  2791.         tempu=_U2; tempv=_V2;
  2792.         _U2=_U3;    _V2=_V3;
  2793.         _U3=tempu; _V3=tempv;
  2794.     }    
  2795.  
  2796.     if (_Y3<_MinClipY || _Y1>_MaxClipY ||
  2797.        (_X1<_MinClipX && _X2<_MinClipX && _X3<_MinClipX) ||
  2798.        (_X1>_MaxClipX && _X2>_MaxClipX && _X3>_MaxClipX) )
  2799.        return;              // do trivial rejection.
  2800.         
  2801.     GENERAL=FALSE;          // reset cases (for Triangle).
  2802.     Buffer=_RendBuffer;     // save starting point of Buffer.
  2803.     
  2804.     TransTBLptr = TransTBL;
  2805.     TransTBLptr += ( _TransLevel<<16);    // Buffer at level of transparency.
  2806.  
  2807.     if (_Y1==_Y2)
  2808.       goto FLAT_TOP;
  2809.     if (_Y2==_Y3)
  2810.       goto FLAT_BOTTOM;
  2811.     else
  2812.       GENERAL=TRUE;
  2813.          
  2814.     height = 65536/(_Y3-_Y1);
  2815.     slope = (_X3-_X1)*height;               
  2816.     newx  = _X1+( (slope*(_Y2-_Y1))>>16 );
  2817.     newu  = (((_Y2-_Y1)*_U3+(_Y3-_Y2)*_U1)*height)>>16;
  2818.     newv  = (((_Y2-_Y1)*_V3+(_Y3-_Y2)*_V1)*height)>>16;
  2819.  
  2820.     old_X3 = _X3;                            // save values for later.
  2821.     old_Y3 = _Y3;
  2822.     old_U3 = _U3;
  2823.     old_V3 = _V3;
  2824.     
  2825.     _X3    = newx;
  2826.     _Y3    = _Y2;           
  2827.     _U3    = newu;
  2828.     _V3    = newv;
  2829.     
  2830.     FLAT_BOTTOM:
  2831.         
  2832.     if (_X3<_X2)
  2833.     {
  2834.       tempx=_X3; _X3=_X2; _X2=tempx;
  2835.       tempu=_U3; _U3=_U2; _U2=tempu;
  2836.       tempv=_V3; _V3=_V2; _V2=tempv;
  2837.     }
  2838.  
  2839.     height  = 65536/(_Y3-_Y1);
  2840.     LeftDx  = (_X2-_X1)*height;    // Inverse left and right slope.
  2841.     RightDx = (_X3-_X1)*height;
  2842.     LeftDu  = (_U2-_U1)*height;
  2843.     LeftDv  = (_V2-_V1)*height;
  2844.     RightDu = (_U3-_U1)*height;
  2845.     RightDv = (_V3-_V1)*height;
  2846.  
  2847.     LeftX   = _X1<<16;
  2848.     RightX  = LeftX+32768;
  2849.  
  2850.     LeftU   = _U1<<16;
  2851.     LeftV   = _V1<<16;
  2852.     RightU  = LeftU;
  2853.     RightV  = LeftV;
  2854.  
  2855.     ScanU  =  0;
  2856.     ScanV  =  0;
  2857.  
  2858.     ClipLeftX     = ( LeftX>>16 );
  2859.     ClipRightX    = ( RightX>>16 );
  2860.  
  2861.     if (_Y1 < _MinClipY)
  2862.     {
  2863.        ydiff  = (_MinClipY - _Y1);
  2864.        LeftX  = LeftX+LeftDx*ydiff;
  2865.        RightX = RightX+RightDx*ydiff;
  2866.  
  2867.        LeftU  = LeftU+LeftDu*ydiff;
  2868.        RightU = RightU+RightDu*ydiff;
  2869.        LeftV  = LeftV+LeftDv*ydiff;
  2870.        RightV = RightV+RightDv*ydiff;
  2871.  
  2872.        ClipLeftX   = ( LeftX>>16 );
  2873.        ClipRightX  = ( RightX>>16 );
  2874.        width       = 1+ClipRightX-ClipLeftX;
  2875.        
  2876.        ScanU  =  (RightU-LeftU)/width;
  2877.        ScanV  =  (RightV-LeftV)/width;            
  2878.  
  2879.            _Y1 = _MinClipY;
  2880.     }
  2881.                 
  2882.     if (_Y3 > _MaxClipY)
  2883.       _Y3 = _MaxClipY;              
  2884.  
  2885.     Buffer+=(_Y1<<8)+(_Y1<<6);
  2886.  
  2887.     if (_X1>=_MinClipX && _X1<=_MaxClipX &&
  2888.         _X2>=_MinClipX && _X2<=_MaxClipX &&
  2889.         _X3>=_MinClipX && _X3<=_MaxClipX)
  2890.     {                   
  2891.        for (y=_Y1;y<_Y3;y++,Buffer+=SCREENWIDTH)    
  2892.        {
  2893.           u=LeftU;
  2894.           v=LeftV;
  2895.           for (x=( ClipLeftX );x<=( ClipRightX );x++)
  2896.           {                 
  2897.              Buffer[x]=TransTBLptr[ ( LookPal[ TextureMap[ (u>>16)+( (v>>16)<<8 ) ] + _ColorIndex ]<<8 ) + Buffer[x] ];
  2898.              u+=ScanU;
  2899.              v+=ScanV;
  2900.           }
  2901.           LeftX  += LeftDx;
  2902.           RightX += RightDx;
  2903.           LeftU  += LeftDu;
  2904.           LeftV  += LeftDv;
  2905.           RightU += RightDu;
  2906.           RightV += RightDv;
  2907.  
  2908.           ClipLeftX  = ( LeftX>>16 );
  2909.           ClipRightX = ( RightX>>16 );
  2910.           width      = 1+ClipRightX-ClipLeftX;
  2911.  
  2912.           ScanU  =  (RightU-LeftU)/width;
  2913.           ScanV  =  (RightV-LeftV)/width;            
  2914.        }
  2915.     }
  2916.     else
  2917.     {
  2918.        for (y=_Y1;y<_Y3;y++,Buffer+=SCREENWIDTH)    
  2919.        {
  2920.  
  2921.           u=LeftU;
  2922.           v=LeftV;
  2923.  
  2924.           if (ClipLeftX < _MinClipX)
  2925.           {
  2926.             if (ClipRightX <= _MinClipX)
  2927.             {
  2928.                LeftX  += LeftDx;      // update the intensities and slopes.
  2929.                RightX += RightDx;     // before continuing.
  2930.                LeftU  += LeftDu;
  2931.                LeftV  += LeftDv;
  2932.                RightU += RightDu;
  2933.                RightV += RightDv;
  2934.  
  2935.                ClipLeftX  = ( LeftX>>16 );
  2936.                ClipRightX = ( RightX>>16 );
  2937.  
  2938.                continue;
  2939.             }
  2940.             u         = LeftU + (_MinClipX - ClipLeftX)*ScanU;
  2941.             v         = LeftV + (_MinClipX - ClipLeftX)*ScanV;
  2942.             ClipLeftX = _MinClipX;     // the clipped amount.
  2943.           }
  2944.  
  2945.           if (ClipRightX > _MaxClipX)
  2946.           {
  2947.             if (ClipLeftX > _MaxClipX)
  2948.             {
  2949.                LeftX  += LeftDx;      // update the intensities and slopes.
  2950.                RightX += RightDx;     // before continuing.
  2951.                LeftU  += LeftDu;
  2952.                LeftV  += LeftDv;
  2953.                RightU += RightDu;
  2954.                RightV += RightDv;
  2955.  
  2956.                ClipLeftX  = ( LeftX>>16 );
  2957.                ClipRightX = ( RightX>>16 );
  2958.  
  2959.                continue;
  2960.             }
  2961.             ClipRightX = _MaxClipX;
  2962.           }
  2963.                                             
  2964.           for (x=ClipLeftX;x<ClipRightX;x++)
  2965.           {
  2966.              Buffer[x]=TransTBLptr[ ( LookPal[ TextureMap[ (u>>16)+( (v>>16)<<8 ) ] + _ColorIndex ]<<8 ) + Buffer[x] ];
  2967.              u+=ScanU;
  2968.              v+=ScanV;
  2969.           }               
  2970.           LeftX  += LeftDx;
  2971.           RightX += RightDx;
  2972.           LeftU  += LeftDu;
  2973.           LeftV  += LeftDv;
  2974.           RightU += RightDu;
  2975.           RightV += RightDv;
  2976.  
  2977.           ClipLeftX  = ( LeftX>>16 );
  2978.           ClipRightX = ( RightX>>16 );
  2979.           width      = 1+ClipRightX-ClipLeftX;
  2980.  
  2981.           ScanU  =  (RightU-LeftU)/width;
  2982.           ScanV  =  (RightV-LeftV)/width;            
  2983.        }
  2984.     }
  2985.  
  2986.     if (!GENERAL)
  2987.       return;
  2988.  
  2989.     _X1     = _X2;                 // setup for FLAT_TOP.
  2990.     _Y1     = _Y2;
  2991.     _U1     = _U2;
  2992.     _V1     = _V2;
  2993.  
  2994.     _X2     = _X3;
  2995.     _U2     = _U3;
  2996.     _V2     = _V3;
  2997.  
  2998.     _X3     = old_X3;
  2999.     _Y3     = old_Y3;
  3000.     _U3     = old_U3;
  3001.     _V3     = old_V3;
  3002.     
  3003.     Buffer=_RendBuffer;     // save starting point of Buffer.
  3004.     
  3005.     FLAT_TOP: 
  3006.  
  3007.     if (_X2<_X1)
  3008.     {
  3009.       tempx=_X2; _X2=_X1; _X1=tempx;
  3010.       tempu=_U2; _U2=_U1; _U1=tempu;
  3011.       tempv=_V2; _V2=_V1; _V1=tempv;
  3012.     }
  3013.  
  3014.     height    = 65536/(_Y3-_Y1);
  3015.  
  3016.     LeftDx    = (_X3-_X1)*height;    // Inverse left and right slope.
  3017.     RightDx   = (_X3-_X2)*height;
  3018.     LeftDu    = (_U3-_U1)*height;
  3019.     LeftDv    = (_V3-_V1)*height;
  3020.     RightDu   = (_U3-_U2)*height;
  3021.     RightDv   = (_V3-_V2)*height;
  3022.  
  3023.     LeftX     = _X1<<16;
  3024.     RightX    = (_X2<<16)+32768;
  3025.     LeftU     = _U1<<16;
  3026.     LeftV     = _V1<<16;
  3027.     RightU    = _U2<<16;
  3028.     RightV    = _V2<<16;
  3029.  
  3030.     ClipLeftX  = ( LeftX>>16 );
  3031.     ClipRightX = ( RightX>>16 );
  3032.     width      = 1+ClipRightX-ClipLeftX;
  3033.  
  3034.     ScanU     = (RightU-LeftU)/width;
  3035.     ScanV     = (RightV-LeftV)/width;            
  3036.                        
  3037.     if (_Y1 < _MinClipY)
  3038.     {
  3039.        ydiff  = (_MinClipY - _Y1);   
  3040.        LeftX  = LeftX+LeftDx*ydiff;
  3041.        RightX = RightX+RightDx*ydiff;
  3042.  
  3043.        LeftU  = LeftU+LeftDu*ydiff;
  3044.        RightU = RightU+RightDu*ydiff;
  3045.        LeftV  = LeftV+LeftDv*ydiff;
  3046.        RightV = RightV+RightDv*ydiff;
  3047.  
  3048.        ClipLeftX  = ( LeftX>>16 );
  3049.        ClipRightX = ( RightX>>16 );
  3050.        width      = 1+ClipRightX-ClipLeftX;
  3051.  
  3052.        ScanU  =  (RightU-LeftU)/width;
  3053.        ScanV  =  (RightV-LeftV)/width;            
  3054.  
  3055.            _Y1 = _MinClipY;
  3056.     }
  3057.                 
  3058.     if (_Y3 > _MaxClipY)
  3059.       _Y3 = _MaxClipY;
  3060.               
  3061.     Buffer+=(_Y1<<8)+(_Y1<<6);
  3062.  
  3063.     if (_X1>=_MinClipX && _X1<=_MaxClipX &&
  3064.         _X2>=_MinClipX && _X2<=_MaxClipX &&
  3065.         _X3>=_MinClipX && _X3<=_MaxClipX)
  3066.     {                   
  3067.        for (y=_Y1;y<_Y3;y++,Buffer+=SCREENWIDTH)
  3068.        {
  3069.           u=LeftU;
  3070.           v=LeftV;
  3071.           for (x=( ClipLeftX );x<=( ClipRightX );x++)
  3072.           {                 
  3073.              Buffer[x]=TransTBLptr[ ( LookPal[ TextureMap[ (u>>16)+( (v>>16)<<8 ) ] + _ColorIndex ]<<8 ) + Buffer[x] ];
  3074.              u+=ScanU;
  3075.              v+=ScanV;
  3076.           }
  3077.           LeftX  += LeftDx;
  3078.           RightX += RightDx;
  3079.           LeftU  += LeftDu;
  3080.           LeftV  += LeftDv;
  3081.           RightU += RightDu;
  3082.           RightV += RightDv;
  3083.  
  3084.           ClipLeftX  = ( LeftX>>16 );
  3085.           ClipRightX = ( RightX>>16 );
  3086.           width      = 1+ClipRightX-ClipLeftX;
  3087.  
  3088.           ScanU  =  (RightU-LeftU)/width;
  3089.           ScanV  =  (RightV-LeftV)/width;            
  3090.        }
  3091.     }
  3092.     else
  3093.     {
  3094.        for (y=_Y1;y<_Y3;y++,Buffer+=SCREENWIDTH)    
  3095.        {
  3096.  
  3097.           u=LeftU;
  3098.           v=LeftV;
  3099.  
  3100.           if (ClipLeftX < _MinClipX)
  3101.           {
  3102.             if (ClipRightX <= _MinClipX)
  3103.             {
  3104.                LeftX  += LeftDx;      // update the intensities and slopes.
  3105.                RightX += RightDx;     // before continuing.
  3106.                LeftU  += LeftDu;
  3107.                LeftV  += LeftDv;
  3108.                RightU += RightDu;
  3109.                RightV += RightDv;
  3110.  
  3111.                ClipLeftX  = ( LeftX>>16 );
  3112.                ClipRightX = ( RightX>>16 );
  3113.  
  3114.                continue;
  3115.             }
  3116.             u          = LeftU + (_MinClipX - ClipLeftX)*ScanU;
  3117.             v          = LeftV + (_MinClipX - ClipLeftX)*ScanV;
  3118.             ClipLeftX  = _MinClipX;   // the clipped amount.
  3119.           }
  3120.  
  3121.           if (ClipRightX > _MaxClipX)
  3122.           {
  3123.             if (ClipLeftX > _MaxClipX)
  3124.             {
  3125.                LeftX  += LeftDx;      // update the intensities and slopes.
  3126.                RightX += RightDx;     // before continuing.
  3127.                LeftU  += LeftDu;
  3128.                LeftV  += LeftDv;
  3129.                RightU += RightDu;
  3130.                RightV += RightDv;
  3131.  
  3132.                ClipLeftX  = ( LeftX>>16 );
  3133.                ClipRightX = ( RightX>>16 );
  3134.  
  3135.                continue;
  3136.             }
  3137.             ClipRightX = _MaxClipX;
  3138.           }
  3139.                                             
  3140.           for (x=ClipLeftX;x<ClipRightX;x++)
  3141.           {
  3142.              Buffer[x]=TransTBLptr[ ( LookPal[ TextureMap[ (u>>16)+( (v>>16)<<8 ) ] + _ColorIndex ]<<8 ) + Buffer[x] ];
  3143.              u+=ScanU;
  3144.              v+=ScanV;
  3145.           }               
  3146.           LeftX  += LeftDx;
  3147.           RightX += RightDx;
  3148.           LeftU  += LeftDu;
  3149.           LeftV  += LeftDv;
  3150.           RightU += RightDu;
  3151.           RightV += RightDv;
  3152.  
  3153.           ClipLeftX  = ( LeftX>>16 );
  3154.           ClipRightX = ( RightX>>16 );
  3155.           width      = 1+ClipRightX-ClipLeftX;
  3156.  
  3157.           ScanU  =  (RightU-LeftU)/width;
  3158.           ScanV  =  (RightV-LeftV)/width;            
  3159.        }
  3160.     }
  3161. }
  3162.  
  3163. void Scan_Convert_TextureGT(void)
  3164. {
  3165.   long LeftX, RightX, LeftDx, RightDx;
  3166.   long height,width,slope;
  3167.   long u,v,ScanU,ScanV,LeftU,LeftV,RightU,RightV,LeftI,RightI,MiddleI;
  3168.   long LeftDu,LeftDv,RightDu,RightDv,LeftDi,RightDi,MiddleDi;
  3169.   int ClipLeftX,ClipRightX;
  3170.   int x,y,ydiff,newi,newx,newu,newv,tempx,tempy,tempu,tempv,tempi;
  3171.   int old_X3,old_Y3,old_U3,old_V3,old_I3;
  3172.   int GENERAL;
  3173.   unsigned char *Buffer;
  3174.   unsigned char *TransTBLptr;
  3175.     
  3176.     if ( (_X1==_X2 && _X2==_X3) || (_Y1==_Y2 && _Y2==_Y3) )
  3177.       return;               // degenerated into lines.       
  3178.  
  3179.     if (_Y2<_Y1)              // switch. They're in the wrong order.
  3180.     {
  3181.         tempx=_X1; tempy=_Y1;
  3182.         _X1=_X2;    _Y1=_Y2;
  3183.         _X2=tempx; _Y2=tempy;
  3184.  
  3185.         tempu=_U1; tempv=_V1; tempi=_I1;
  3186.         _U1=_U2;    _V1=_V2;    _I1=_I2;
  3187.         _U2=tempu; _V2=tempv; _I2=tempi;
  3188.     }    
  3189.     if (_Y3<_Y1)              // switch. They're in the wrong order.
  3190.     {
  3191.         tempx=_X1; tempy=_Y1;
  3192.         _X1=_X3;    _Y1=_Y3;
  3193.         _X3=tempx; _Y3=tempy;
  3194.  
  3195.         tempu=_U1; tempv=_V1; tempi=_I1;
  3196.         _U1=_U3;    _V1=_V3;    _I1=_I3;
  3197.         _U3=tempu; _V3=tempv; _I3=tempi;
  3198.     }
  3199.     if (_Y3<_Y2)              // switch. They're in the wrong order.
  3200.     {
  3201.         tempx=_X2; tempy=_Y2;
  3202.         _X2=_X3;    _Y2=_Y3;
  3203.         _X3=tempx; _Y3=tempy;
  3204.  
  3205.         tempu=_U2; tempv=_V2; tempi=_I2;
  3206.         _U2=_U3;    _V2=_V3;    _I2=_I3;
  3207.         _U3=tempu; _V3=tempv; _I3=tempi;
  3208.     }    
  3209.  
  3210.     if (_Y3<_MinClipY || _Y1>_MaxClipY ||
  3211.        (_X1<_MinClipX && _X2<_MinClipX && _X3<_MinClipX) ||
  3212.        (_X1>_MaxClipX && _X2>_MaxClipX && _X3>_MaxClipX) )
  3213.        return;              // do trivial rejection.
  3214.         
  3215.     GENERAL=FALSE;          // reset cases (for Triangle).
  3216.     Buffer=_RendBuffer;     // save starting point of Buffer.
  3217.  
  3218.     TransTBLptr=TransTBL;
  3219.     TransTBLptr+=( _TransLevel<<16);
  3220.        
  3221.     if (_Y1==_Y2)
  3222.       goto FLAT_TOP;
  3223.     if (_Y2==_Y3)
  3224.       goto FLAT_BOTTOM;
  3225.     else
  3226.       GENERAL=TRUE;
  3227.          
  3228.     height  = 65536/(_Y3-_Y1);
  3229.     slope   = (_X3-_X1)*height;               
  3230.     newx    = _X1+( (slope*(_Y2-_Y1))>>16 );
  3231.     newu    = (((_Y2-_Y1)*_U3+(_Y3-_Y2)*_U1)*height)>>16;
  3232.     newv    = (((_Y2-_Y1)*_V3+(_Y3-_Y2)*_V1)*height)>>16;
  3233.     newi    = (((_Y2-_Y1)*_I3+(_Y3-_Y2)*_I1)*height)>>16;
  3234.  
  3235.     old_X3   = _X3;                            // save values for later.
  3236.     old_Y3   = _Y3;
  3237.     old_U3   = _U3;
  3238.     old_V3   = _V3;
  3239.     old_I3   = _I3;       
  3240.     
  3241.     _X3      = newx;
  3242.     _Y3      = _Y2;           
  3243.     _U3      = newu;
  3244.     _V3      = newv;
  3245.     _I3      = newi;
  3246.     
  3247.     FLAT_BOTTOM:
  3248.  
  3249.     if (_X3<_X2)
  3250.     {
  3251.       tempx=_X3; _X3=_X2; _X2=tempx;
  3252.       tempu=_U3; _U3=_U2; _U2=tempu;
  3253.       tempv=_V3; _V3=_V2; _V2=tempv;
  3254.       tempi=_I3; _I3=_I2; _I2=tempi;
  3255.     }
  3256.  
  3257.     height  = 65536/(_Y3-_Y1);
  3258.     LeftDx  = (_X2-_X1)*height;    // Inverse left and right slope.
  3259.     RightDx = (_X3-_X1)*height;
  3260.     LeftDu  = (_U2-_U1)*height;
  3261.     LeftDv  = (_V2-_V1)*height;
  3262.     RightDu = (_U3-_U1)*height;
  3263.     RightDv = (_V3-_V1)*height;
  3264.     LeftDi  = (_I2-_I1)*height;  // compute intensity deltas.
  3265.     RightDi = (_I3-_I1)*height; 
  3266.  
  3267.     LeftX   = _X1<<16;
  3268.     RightX  = LeftX+32768;
  3269.     LeftU   = _U1<<16;
  3270.     LeftV   = _V1<<16;
  3271.     RightU  = LeftU;
  3272.     RightV  = LeftV;
  3273.     LeftI   = _I1<<16;              // assign intensity to left and right side.
  3274.     RightI  = LeftI;
  3275.  
  3276.     ScanU    = 0;              // because of Flat bottom.
  3277.     ScanV    = 0;
  3278.     MiddleDi = 0;
  3279.  
  3280.     ClipLeftX  = ( LeftX>>16 );
  3281.     ClipRightX = ( RightX>>16 );
  3282.  
  3283.     if (_Y1 < _MinClipY)
  3284.     {
  3285.        ydiff    = (_MinClipY - _Y1);
  3286.        LeftX    = LeftX+LeftDx*ydiff;
  3287.        RightX   = RightX+RightDx*ydiff;
  3288.  
  3289.        LeftU    = LeftU+LeftDu*ydiff;
  3290.        RightU   = RightU+RightDu*ydiff;
  3291.        LeftV    = LeftV+LeftDv*ydiff;
  3292.        RightV   = RightV+RightDv*ydiff;
  3293.  
  3294.        LeftI    = LeftI+LeftDi*ydiff;
  3295.        RightI   = RightI+RightDi*ydiff;
  3296.  
  3297.        ClipLeftX  = ( LeftX>>16 );
  3298.        ClipRightX = ( RightX>>16 );
  3299.        width      = 1+ClipRightX-ClipLeftX;
  3300.      
  3301.        ScanU    = (RightU-LeftU)/width;
  3302.        ScanV    = (RightV-LeftV)/width;            
  3303.        MiddleDi = (RightI-LeftI)/width;          
  3304.  
  3305.            _Y1 = _MinClipY;
  3306.     }
  3307.                 
  3308.     if (_Y3 > _MaxClipY)
  3309.       _Y3 = _MaxClipY;
  3310.               
  3311.     Buffer+=(_Y1<<8)+(_Y1<<6);
  3312.  
  3313.     if (_X1>=_MinClipX && _X1<=_MaxClipX &&
  3314.         _X2>=_MinClipX && _X2<=_MaxClipX &&
  3315.         _X3>=_MinClipX && _X3<=_MaxClipX)
  3316.     {                   
  3317.        for (y=_Y1;y<_Y3;y++,Buffer+=SCREENWIDTH)    
  3318.        {
  3319.           u=LeftU;
  3320.           v=LeftV;
  3321.           MiddleI=LeftI;
  3322.           for (x=( ClipLeftX );x<=( ClipRightX );x++)
  3323.           {
  3324.              Buffer[x]=TransTBLptr[ ( LookPal[ TextureMap[ (u>>16)+( (v>>16)<<8 ) ] + (MiddleI>>16) ]<<8 ) + Buffer[x] ];
  3325.              u+=ScanU;
  3326.              v+=ScanV;
  3327.              MiddleI+=MiddleDi;
  3328.           }       
  3329.           LeftX   += LeftDx;
  3330.           RightX  += RightDx;
  3331.           LeftU   += LeftDu;
  3332.           LeftV   += LeftDv;
  3333.           RightU  += RightDu;
  3334.           RightV  += RightDv;
  3335.           LeftI   += LeftDi;      // update the intensities and slopes.
  3336.           RightI  += RightDi;
  3337.  
  3338.           ClipLeftX  = ( LeftX>>16 );
  3339.           ClipRightX = ( RightX>>16 );
  3340.           width      = 1+ClipRightX-ClipLeftX;
  3341.      
  3342.           ScanU    = (RightU-LeftU)/width;
  3343.           ScanV    = (RightV-LeftV)/width;            
  3344.           MiddleDi = (RightI-LeftI)/width;          
  3345.        }
  3346.     }
  3347.     else
  3348.     {
  3349.        for (y=_Y1;y<_Y3;y++,Buffer+=SCREENWIDTH)    
  3350.        {
  3351.  
  3352.           u=LeftU;
  3353.           v=LeftV;
  3354.           MiddleI=LeftI;                                            
  3355.  
  3356.           if (ClipLeftX < _MinClipX)
  3357.           {
  3358.             if (ClipRightX <= _MinClipX)
  3359.             {
  3360.                LeftX  += LeftDx;      // update the intensities and slopes.
  3361.                RightX += RightDx;     // before continuing.
  3362.                LeftU  += LeftDu;
  3363.                LeftV  += LeftDv;
  3364.                RightU += RightDu;
  3365.                RightV += RightDv;
  3366.                LeftI   += LeftDi;      
  3367.                RightI  += RightDi;         
  3368.  
  3369.                ClipLeftX  = ( LeftX>>16 );
  3370.                ClipRightX = ( RightX>>16 );
  3371.  
  3372.                continue;
  3373.             }
  3374.             u         = LeftU + (_MinClipX - ClipLeftX)*ScanU;
  3375.             v         = LeftV + (_MinClipX - ClipLeftX)*ScanV;
  3376.             MiddleI   = LeftI + (_MinClipX - ClipLeftX)*MiddleDi;   // move the intensity by  
  3377.             ClipLeftX = _MinClipX;     // the clipped amount.
  3378.           }
  3379.  
  3380.           if (ClipRightX > _MaxClipX)
  3381.           {
  3382.             if (ClipLeftX > _MaxClipX)
  3383.             {
  3384.                LeftX  += LeftDx;      // update the intensities and slopes.
  3385.                RightX += RightDx;     // before continuing.
  3386.                LeftU  += LeftDu;
  3387.                LeftV  += LeftDv;
  3388.                RightU += RightDu;
  3389.                RightV += RightDv;
  3390.                LeftI  += LeftDi;      
  3391.                RightI += RightDi;         
  3392.  
  3393.                ClipLeftX  = ( LeftX>>16 );
  3394.                ClipRightX = ( RightX>>16 );
  3395.  
  3396.                continue;
  3397.             }
  3398.             ClipRightX = _MaxClipX;
  3399.           }
  3400.           
  3401.           for (x=ClipLeftX;x<ClipRightX;x++)
  3402.           {
  3403.              Buffer[x]=TransTBLptr[ ( LookPal[ TextureMap[ (u>>16)+( (v>>16)<<8 ) ] + (MiddleI>>16) ]<<8 ) + Buffer[x] ];
  3404.              u+=ScanU;
  3405.              v+=ScanV;
  3406.              MiddleI+=MiddleDi;
  3407.           }               
  3408.           LeftX   += LeftDx;
  3409.           RightX  += RightDx;
  3410.           LeftU   += LeftDu;
  3411.           LeftV   += LeftDv;
  3412.           RightU  += RightDu;
  3413.           RightV  += RightDv;
  3414.           LeftI   += LeftDi;   
  3415.           RightI  += RightDi;         
  3416.  
  3417.           ClipLeftX  = ( LeftX>>16 );
  3418.           ClipRightX = ( RightX>>16 );
  3419.           width      = 1+ClipRightX-ClipLeftX;
  3420.      
  3421.           ScanU    = (RightU-LeftU)/width;
  3422.           ScanV    = (RightV-LeftV)/width;            
  3423.           MiddleDi = (RightI-LeftI)/width;          
  3424.        }
  3425.     }
  3426.  
  3427.     if (!GENERAL)
  3428.       return;
  3429.  
  3430.     _X1 = _X2;                 // setup for FLAT_TOP.
  3431.     _Y1 = _Y2;
  3432.     _U1 = _U2;
  3433.     _V1 = _V2;
  3434.     _I1 = _I2;
  3435.  
  3436.     _X2 = _X3;
  3437.     _U2 = _U3;
  3438.     _V2 = _V3;
  3439.     _I2 = _I3;
  3440.  
  3441.     _X3 = old_X3;
  3442.     _Y3 = old_Y3;
  3443.     _U3 = old_U3;
  3444.     _V3 = old_V3;
  3445.     _I3 = old_I3;
  3446.     
  3447.     Buffer=_RendBuffer;     // save starting point of Buffer.
  3448.     
  3449.     FLAT_TOP: 
  3450.  
  3451.     if (_X2<_X1)
  3452.     {
  3453.       tempx=_X2; _X2=_X1; _X1=tempx;
  3454.       tempu=_U2; _U2=_U1; _U1=tempu;
  3455.       tempv=_V2; _V2=_V1; _V1=tempv;
  3456.       tempi=_I2; _I2=_I1; _I1=tempi;
  3457.     }
  3458.  
  3459.     height    = 65536/(_Y3-_Y1);
  3460.  
  3461.     LeftDx    = (_X3-_X1)*height;    // Inverse left and right slope.
  3462.     RightDx   = (_X3-_X2)*height;
  3463.     LeftDu    = (_U3-_U1)*height;
  3464.     LeftDv    = (_V3-_V1)*height;
  3465.     RightDu   = (_U3-_U2)*height;
  3466.     RightDv   = (_V3-_V2)*height;
  3467.     LeftDi    = (_I3-_I1)*height;  // compute intensity deltas.
  3468.     RightDi   = (_I3-_I2)*height;
  3469.     
  3470.     LeftX     = _X1<<16;
  3471.     RightX    = (_X2<<16)+32768;
  3472.     LeftU     = _U1<<16;
  3473.     LeftV     = _V1<<16;
  3474.     RightU    = _U2<<16;
  3475.     RightV    = _V2<<16;
  3476.     LeftI     = _I1<<16;              // assign intensity to left and right side.
  3477.     RightI    = _I2<<16;
  3478.  
  3479.     ClipLeftX  = ( LeftX>>16 );
  3480.     ClipRightX = ( RightX>>16 );
  3481.     width      = 1+ClipRightX-ClipLeftX;
  3482.     
  3483.     ScanU    = (RightU-LeftU)/width;
  3484.     ScanV    = (RightV-LeftV)/width;            
  3485.     MiddleDi = (RightI-LeftI)/width;          
  3486.                        
  3487.     if (_Y1 < _MinClipY)
  3488.     {
  3489.        ydiff    = (_MinClipY - _Y1);
  3490.        LeftX    = LeftX+LeftDx*ydiff;
  3491.        RightX   = RightX+RightDx*ydiff;
  3492.  
  3493.        LeftU    = LeftU+LeftDu*ydiff;
  3494.        RightU   = RightU+RightDu*ydiff;
  3495.        LeftV    = LeftV+LeftDv*ydiff;
  3496.        RightV   = RightV+RightDv*ydiff;
  3497.  
  3498.        LeftI    = LeftI+LeftDi*ydiff;
  3499.        RightI   = RightI+RightDi*ydiff;
  3500.  
  3501.        ClipLeftX  = ( LeftX>>16 );
  3502.        ClipRightX = ( RightX>>16 );
  3503.        width      = 1+ClipRightX-ClipLeftX;
  3504.     
  3505.        ScanU    = (RightU-LeftU)/width;
  3506.        ScanV    = (RightV-LeftV)/width;            
  3507.        MiddleDi = (RightI-LeftI)/width;          
  3508.  
  3509.              _Y1 = _MinClipY;
  3510.     }
  3511.                 
  3512.     if (_Y3 > _MaxClipY)
  3513.       _Y3 = _MaxClipY;
  3514.               
  3515.     Buffer+=(_Y1<<8)+(_Y1<<6);
  3516.  
  3517.     if (_X1>=_MinClipX && _X1<=_MaxClipX &&
  3518.         _X2>=_MinClipX && _X2<=_MaxClipX &&
  3519.         _X3>=_MinClipX && _X3<=_MaxClipX)
  3520.     {                   
  3521.        for (y=_Y1;y<_Y3;y++,Buffer+=SCREENWIDTH)    
  3522.        {        
  3523.           u=LeftU;
  3524.           v=LeftV;
  3525.           MiddleI=LeftI;
  3526.           for (x=( ClipLeftX );x<=( ClipRightX );x++)
  3527.           {                 
  3528.              Buffer[x]=TransTBLptr[ ( LookPal[ TextureMap[ (u>>16)+( (v>>16)<<8 ) ] + (MiddleI>>16) ]<<8 ) + Buffer[x] ];
  3529.              u+=ScanU;
  3530.              v+=ScanV;
  3531.              MiddleI+=MiddleDi;
  3532.           }
  3533.           LeftX   += LeftDx;
  3534.           RightX  += RightDx;
  3535.           LeftU   += LeftDu;
  3536.           LeftV   += LeftDv;
  3537.           RightU  += RightDu;
  3538.           RightV  += RightDv;
  3539.           LeftI   += LeftDi;      // update the intensities and slopes.
  3540.           RightI  += RightDi;
  3541.  
  3542.           ClipLeftX  = ( LeftX>>16 );
  3543.           ClipRightX = ( RightX>>16 );
  3544.           width      = 1+ClipRightX-ClipLeftX;
  3545.     
  3546.           ScanU    = (RightU-LeftU)/width;
  3547.           ScanV    = (RightV-LeftV)/width;            
  3548.           MiddleDi = (RightI-LeftI)/width;          
  3549.        }
  3550.     }
  3551.     else
  3552.     {
  3553.        for (y=_Y1;y<_Y3;y++,Buffer+=SCREENWIDTH)
  3554.        {
  3555.  
  3556.           u=LeftU;
  3557.           v=LeftV;
  3558.           MiddleI=LeftI;
  3559.           if (ClipLeftX < _MinClipX)
  3560.           {
  3561.             if (ClipRightX <= _MinClipX)
  3562.             {
  3563.                LeftX  += LeftDx;      // update the intensities and slopes.
  3564.                RightX += RightDx;     // before continuing.
  3565.                LeftU  += LeftDu;
  3566.                LeftV  += LeftDv;
  3567.                RightU += RightDu;
  3568.                RightV += RightDv;
  3569.                LeftI  += LeftDi;      
  3570.                RightI += RightDi;         
  3571.  
  3572.                ClipLeftX  = ( LeftX>>16 );
  3573.                ClipRightX = ( RightX>>16 );
  3574.  
  3575.                continue;
  3576.             }
  3577.             u          = LeftU + (_MinClipX - ClipLeftX)*ScanU;
  3578.             v          = LeftV + (_MinClipX - ClipLeftX)*ScanV;
  3579.             MiddleI    = LeftI + (_MinClipX - ClipLeftX)*MiddleDi;   // move the intensity by
  3580.             ClipLeftX  = _MinClipX;   // the clipped amount.
  3581.           }
  3582.  
  3583.           if (ClipRightX > _MaxClipX)
  3584.           {
  3585.             if (ClipLeftX > _MaxClipX)
  3586.             {
  3587.                LeftX  += LeftDx;      // update the intensities and slopes.
  3588.                RightX += RightDx;     // before continuing.
  3589.                LeftU  += LeftDu;
  3590.                LeftV  += LeftDv;
  3591.                RightU += RightDu;
  3592.                RightV += RightDv;
  3593.                LeftI  += LeftDi;      
  3594.                RightI += RightDi;
  3595.  
  3596.                ClipLeftX  = ( LeftX>>16 );
  3597.                ClipRightX = ( RightX>>16 );
  3598.  
  3599.                continue;
  3600.             }
  3601.             ClipRightX = _MaxClipX;
  3602.           }
  3603.                                             
  3604.           for (x=ClipLeftX;x<ClipRightX;x++)
  3605.           {
  3606.              Buffer[x]=TransTBLptr[ ( LookPal[ TextureMap[ (u>>16)+( (v>>16)<<8 ) ] + (MiddleI>>16) ]<<8 ) + Buffer[x] ];
  3607.              u+=ScanU;
  3608.              v+=ScanV;
  3609.              MiddleI+=MiddleDi;
  3610.           }               
  3611.           LeftX   += LeftDx;
  3612.           RightX  += RightDx;
  3613.           LeftU   += LeftDu;
  3614.           LeftV   += LeftDv;
  3615.           RightU  += RightDu;
  3616.           RightV  += RightDv;
  3617.           LeftI   += LeftDi;     
  3618.           RightI  += RightDi;         
  3619.  
  3620.           ClipLeftX  = ( LeftX>>16 );
  3621.           ClipRightX = ( RightX>>16 );
  3622.           width      = 1+ClipRightX-ClipLeftX;
  3623.     
  3624.           ScanU    = (RightU-LeftU)/width;
  3625.           ScanV    = (RightV-LeftV)/width;            
  3626.           MiddleDi = (RightI-LeftI)/width;          
  3627.        }
  3628.     }
  3629. }
  3630.  
  3631. void Scan_Convert_TexturePT(void)
  3632. {
  3633.   long LeftX, RightX, LeftDx, RightDx;
  3634.   long height,width,slope;
  3635.   long u,v,ScanU,ScanV,LeftU,LeftV,RightU,RightV,LeftA,RightA,MiddleA;
  3636.   long LeftDu,LeftDv,RightDu,RightDv,LeftDa,RightDa,MiddleDa;
  3637.   int ClipLeftX,ClipRightX;
  3638.   int x,y,ydiff,newa,newx,newu,newv,tempx,tempy,tempu,tempv,tempa;
  3639.   int old_X3,old_Y3,old_U3,old_V3,old_A3;
  3640.   int GENERAL;
  3641.   unsigned char *Buffer;
  3642.   unsigned char *TransTBLptr;
  3643.     
  3644.     if ( (_X1==_X2 && _X2==_X3) || (_Y1==_Y2 && _Y2==_Y3) )
  3645.       return;               // degenerated into lines.       
  3646.  
  3647.     if (_Y2<_Y1)              // switch. They're in the wrong order.
  3648.     {
  3649.         tempx=_X1; tempy=_Y1;
  3650.         _X1=_X2;    _Y1=_Y2;
  3651.         _X2=tempx; _Y2=tempy;
  3652.  
  3653.         tempu=_U1; tempv=_V1; tempa=_A1;
  3654.         _U1=_U2;    _V1=_V2;    _A1=_A2;
  3655.         _U2=tempu; _V2=tempv; _A2=tempa;
  3656.     }    
  3657.     if (_Y3<_Y1)              // switch. They're in the wrong order.
  3658.     {
  3659.         tempx=_X1; tempy=_Y1;
  3660.         _X1=_X3;    _Y1=_Y3;
  3661.         _X3=tempx; _Y3=tempy;
  3662.  
  3663.         tempu=_U1; tempv=_V1; tempa=_A1;
  3664.         _U1=_U3;    _V1=_V3;    _A1=_A3;
  3665.         _U3=tempu; _V3=tempv; _A3=tempa;
  3666.     }
  3667.     if (_Y3<_Y2)              // switch. They're in the wrong order.
  3668.     {
  3669.         tempx=_X2; tempy=_Y2;
  3670.         _X2=_X3;    _Y2=_Y3;
  3671.         _X3=tempx; _Y3=tempy;
  3672.  
  3673.         tempu=_U2; tempv=_V2; tempa=_A2;
  3674.         _U2=_U3;    _V2=_V3;    _A2=_A3;
  3675.         _U3=tempu; _V3=tempv; _A3=tempa;
  3676.     }    
  3677.  
  3678.     if (_Y3<_MinClipY || _Y1>_MaxClipY ||
  3679.        (_X1<_MinClipX && _X2<_MinClipX && _X3<_MinClipX) ||
  3680.        (_X1>_MaxClipX && _X2>_MaxClipX && _X3>_MaxClipX) )
  3681.        return;              // do trivial rejection.
  3682.         
  3683.     GENERAL=FALSE;          // reset cases (for Triangle).
  3684.     Buffer=_RendBuffer;     // save starting point of Buffer.
  3685.  
  3686.     TransTBLptr=TransTBL;
  3687.     TransTBLptr+=( _TransLevel<<16);
  3688.        
  3689.     if (_Y1==_Y2)
  3690.       goto FLAT_TOP;
  3691.     if (_Y2==_Y3)
  3692.       goto FLAT_BOTTOM;
  3693.     else
  3694.       GENERAL=TRUE;
  3695.          
  3696.     height  = 65536/(_Y3-_Y1);
  3697.     slope   = (_X3-_X1)*height;               
  3698.     newx    = _X1+( (slope*(_Y2-_Y1))>>16 );
  3699.     newu    = (((_Y2-_Y1)*_U3+(_Y3-_Y2)*_U1)*height)>>16;
  3700.     newv    = (((_Y2-_Y1)*_V3+(_Y3-_Y2)*_V1)*height)>>16;
  3701.     newa    = (((_Y2-_Y1)*_A3+(_Y3-_Y2)*_A1)*height)>>16;
  3702.  
  3703.     old_X3   = _X3;                            // save values for later.
  3704.     old_Y3   = _Y3;
  3705.     old_U3   = _U3;
  3706.     old_V3   = _V3;
  3707.     old_A3   = _A3;       
  3708.     
  3709.     _X3      = newx;
  3710.     _Y3      = _Y2;           
  3711.     _U3      = newu;
  3712.     _V3      = newv;
  3713.     _A3      = newa;
  3714.     
  3715.     FLAT_BOTTOM:
  3716.  
  3717.     if (_X3<_X2)
  3718.     {
  3719.       tempx=_X3; _X3=_X2; _X2=tempx;
  3720.       tempu=_U3; _U3=_U2; _U2=tempu;
  3721.       tempv=_V3; _V3=_V2; _V2=tempv;
  3722.       tempa=_A3; _A3=_A2; _A2=tempa;
  3723.     }
  3724.  
  3725.     height  = 65536/(_Y3-_Y1);
  3726.     LeftDx  = (_X2-_X1)*height;    // Inverse left and right slope.
  3727.     RightDx = (_X3-_X1)*height;
  3728.     LeftDu  = (_U2-_U1)*height;
  3729.     LeftDv  = (_V2-_V1)*height;
  3730.     RightDu = (_U3-_U1)*height;
  3731.     RightDv = (_V3-_V1)*height;
  3732.     LeftDa  = (_A2-_A1)*height;  // compute intensity deltas.
  3733.     RightDa = (_A3-_A1)*height; 
  3734.  
  3735.     LeftX   = _X1<<16;
  3736.     RightX  = LeftX+32768;
  3737.     LeftU   = _U1<<16;
  3738.     LeftV   = _V1<<16;
  3739.     RightU  = LeftU;
  3740.     RightV  = LeftV;
  3741.     LeftA   = _A1<<16;              // assign intensity to left and right side.
  3742.     RightA  = LeftA;
  3743.  
  3744.     ScanU    = 0;              // because of Flat bottom.
  3745.     ScanV    = 0;
  3746.     MiddleDa = 0;
  3747.  
  3748.     ClipLeftX  = ( LeftX>>16 );
  3749.     ClipRightX = ( RightX>>16 );
  3750.  
  3751.     if (_Y1 < _MinClipY)
  3752.     {
  3753.        ydiff    = (_MinClipY - _Y1);
  3754.        LeftX    = LeftX+LeftDx*ydiff;
  3755.        RightX   = RightX+RightDx*ydiff;
  3756.  
  3757.        LeftU    = LeftU+LeftDu*ydiff;
  3758.        RightU   = RightU+RightDu*ydiff;
  3759.        LeftV    = LeftV+LeftDv*ydiff;
  3760.        RightV   = RightV+RightDv*ydiff;
  3761.  
  3762.        LeftA    = LeftA+LeftDa*ydiff;
  3763.        RightA   = RightA+RightDa*ydiff;
  3764.  
  3765.        ClipLeftX  = ( LeftX>>16 );
  3766.        ClipRightX = ( RightX>>16 );
  3767.        width      = 1+ClipRightX-ClipLeftX;
  3768.      
  3769.        ScanU    = (RightU-LeftU)/width;
  3770.        ScanV    = (RightV-LeftV)/width;            
  3771.        MiddleDa = (RightA-LeftA)/width;          
  3772.  
  3773.            _Y1 = _MinClipY;
  3774.     }
  3775.                 
  3776.     if (_Y3 > _MaxClipY)
  3777.       _Y3 = _MaxClipY;
  3778.               
  3779.     Buffer+=(_Y1<<8)+(_Y1<<6);
  3780.  
  3781.     if (_X1>=_MinClipX && _X1<=_MaxClipX &&
  3782.         _X2>=_MinClipX && _X2<=_MaxClipX &&
  3783.         _X3>=_MinClipX && _X3<=_MaxClipX)
  3784.     {                   
  3785.        for (y=_Y1;y<_Y3;y++,Buffer+=SCREENWIDTH)    
  3786.        {
  3787.           u=LeftU;
  3788.           v=LeftV;
  3789.           MiddleA=LeftA;
  3790.           for (x=( ClipLeftX );x<=( ClipRightX );x++)
  3791.           {                 
  3792.              Buffer[x]=TransTBLptr[ ( LookPalPhong[ TextureMap[ (u>>16)+( (v>>16)<<8 ) ] + PhongTBL[ (MiddleA>>16) ] ]<<8 ) + Buffer[x] ];
  3793.              u+=ScanU;
  3794.              v+=ScanV;
  3795.              MiddleA+=MiddleDa;
  3796.           }       
  3797.           LeftX   += LeftDx;
  3798.           RightX  += RightDx;
  3799.           LeftU   += LeftDu;
  3800.           LeftV   += LeftDv;
  3801.           RightU  += RightDu;
  3802.           RightV  += RightDv;
  3803.           LeftA   += LeftDa;      // update the intensities and slopes.
  3804.           RightA  += RightDa;
  3805.  
  3806.           ClipLeftX  = ( LeftX>>16 );
  3807.           ClipRightX = ( RightX>>16 );
  3808.           width      = 1+ClipRightX-ClipLeftX;
  3809.      
  3810.           ScanU    = (RightU-LeftU)/width;
  3811.           ScanV    = (RightV-LeftV)/width;            
  3812.           MiddleDa = (RightA-LeftA)/width;          
  3813.        }
  3814.     }
  3815.     else
  3816.     {
  3817.        for (y=_Y1;y<_Y3;y++,Buffer+=SCREENWIDTH)    
  3818.        {
  3819.  
  3820.           u=LeftU;
  3821.           v=LeftV;
  3822.           MiddleA=LeftA;                                            
  3823.  
  3824.           if (ClipLeftX < _MinClipX)
  3825.           {
  3826.             if (ClipRightX <= _MinClipX)
  3827.             {
  3828.                LeftX  += LeftDx;      // update the intensities and slopes.
  3829.                RightX += RightDx;     // before continuing.
  3830.                LeftU  += LeftDu;
  3831.                LeftV  += LeftDv;
  3832.                RightU += RightDu;
  3833.                RightV += RightDv;
  3834.                LeftA   += LeftDa;      
  3835.                RightA  += RightDa;         
  3836.  
  3837.                ClipLeftX  = ( LeftX>>16 );
  3838.                ClipRightX = ( RightX>>16 );
  3839.  
  3840.                continue;
  3841.             }
  3842.             u         = LeftU + (_MinClipX - ClipLeftX)*ScanU;
  3843.             v         = LeftV + (_MinClipX - ClipLeftX)*ScanV;
  3844.             MiddleA   = LeftA + (_MinClipX - ClipLeftX)*MiddleDa;   // move the intensity by
  3845.             ClipLeftX = _MinClipX;     // the clipped amount.
  3846.           }
  3847.  
  3848.           if (ClipRightX > _MaxClipX)
  3849.           {
  3850.             if (ClipLeftX > _MaxClipX)
  3851.             {
  3852.                LeftX  += LeftDx;      // update the intensities and slopes.
  3853.                RightX += RightDx;     // before continuing.
  3854.                LeftU  += LeftDu;
  3855.                LeftV  += LeftDv;
  3856.                RightU += RightDu;
  3857.                RightV += RightDv;
  3858.                LeftA  += LeftDa;      
  3859.                RightA += RightDa;
  3860.  
  3861.                ClipLeftX  = ( LeftX>>16 );
  3862.                ClipRightX = ( RightX>>16 );
  3863.  
  3864.                continue;
  3865.             }
  3866.             ClipRightX = _MaxClipX;
  3867.           }
  3868.           
  3869.           for (x=ClipLeftX;x<ClipRightX;x++)
  3870.           {
  3871.              Buffer[x]=TransTBLptr[ ( LookPalPhong[ TextureMap[ (u>>16)+( (v>>16)<<8 ) ] + PhongTBL[ (MiddleA>>16) ] ]<<8 ) + Buffer[x] ];
  3872.              u+=ScanU;
  3873.              v+=ScanV;
  3874.              MiddleA+=MiddleDa;
  3875.           }               
  3876.           LeftX   += LeftDx;
  3877.           RightX  += RightDx;
  3878.           LeftU   += LeftDu;
  3879.           LeftV   += LeftDv;
  3880.           RightU  += RightDu;
  3881.           RightV  += RightDv;
  3882.           LeftA   += LeftDa;   
  3883.           RightA  += RightDa;         
  3884.  
  3885.           ClipLeftX  = ( LeftX>>16 );
  3886.           ClipRightX = ( RightX>>16 );
  3887.           width      = 1+ClipRightX-ClipLeftX;
  3888.      
  3889.           ScanU    = (RightU-LeftU)/width;
  3890.           ScanV    = (RightV-LeftV)/width;            
  3891.           MiddleDa = (RightA-LeftA)/width;          
  3892.        }
  3893.     }
  3894.  
  3895.     if (!GENERAL)
  3896.       return;
  3897.  
  3898.     _X1 = _X2;                 // setup for FLAT_TOP.
  3899.     _Y1 = _Y2;
  3900.     _U1 = _U2;
  3901.     _V1 = _V2;
  3902.     _A1 = _A2;
  3903.  
  3904.     _X2 = _X3;
  3905.     _U2 = _U3;
  3906.     _V2 = _V3;
  3907.     _A2 = _A3;
  3908.  
  3909.     _X3 = old_X3;
  3910.     _Y3 = old_Y3;
  3911.     _U3 = old_U3;
  3912.     _V3 = old_V3;
  3913.     _A3 = old_A3;
  3914.     
  3915.     Buffer=_RendBuffer;     // save starting point of Buffer.
  3916.     
  3917.     FLAT_TOP: 
  3918.  
  3919.     if (_X2<_X1)
  3920.     {
  3921.       tempx=_X2; _X2=_X1; _X1=tempx;
  3922.       tempu=_U2; _U2=_U1; _U1=tempu;
  3923.       tempv=_V2; _V2=_V1; _V1=tempv;
  3924.       tempa=_A2; _A2=_A1; _A1=tempa;
  3925.     }
  3926.  
  3927.     height    = 65536/(_Y3-_Y1);
  3928.  
  3929.     LeftDx    = (_X3-_X1)*height;    // Inverse left and right slope.
  3930.     RightDx   = (_X3-_X2)*height;
  3931.     LeftDu    = (_U3-_U1)*height;
  3932.     LeftDv    = (_V3-_V1)*height;
  3933.     RightDu   = (_U3-_U2)*height;
  3934.     RightDv   = (_V3-_V2)*height;
  3935.     LeftDa    = (_A3-_A1)*height;  // compute intensity deltas.
  3936.     RightDa   = (_A3-_A2)*height;
  3937.     
  3938.     LeftX     = _X1<<16;
  3939.     RightX    = (_X2<<16)+32768;
  3940.     LeftU     = _U1<<16;
  3941.     LeftV     = _V1<<16;
  3942.     RightU    = _U2<<16;
  3943.     RightV    = _V2<<16;
  3944.     LeftA     = _A1<<16;              // assign intensity to left and right side.
  3945.     RightA    = _A2<<16;
  3946.  
  3947.     ClipLeftX  = ( LeftX>>16 );
  3948.     ClipRightX = ( RightX>>16 );
  3949.     width      = 1+ClipRightX-ClipLeftX;
  3950.     
  3951.     ScanU    = (RightU-LeftU)/width;
  3952.     ScanV    = (RightV-LeftV)/width;            
  3953.     MiddleDa = (RightA-LeftA)/width;          
  3954.                        
  3955.     if (_Y1 < _MinClipY)
  3956.     {
  3957.        ydiff    = (_MinClipY - _Y1);
  3958.        LeftX    = LeftX+LeftDx*ydiff;
  3959.        RightX   = RightX+RightDx*ydiff;
  3960.  
  3961.        LeftU    = LeftU+LeftDu*ydiff;
  3962.        RightU   = RightU+RightDu*ydiff;
  3963.        LeftV    = LeftV+LeftDv*ydiff;
  3964.        RightV   = RightV+RightDv*ydiff;
  3965.  
  3966.        LeftA    = LeftA+LeftDa*ydiff;
  3967.        RightA   = RightA+RightDa*ydiff;
  3968.  
  3969.        ClipLeftX  = ( LeftX>>16 );
  3970.        ClipRightX = ( RightX>>16 );
  3971.        width      = 1+ClipRightX-ClipLeftX;
  3972.     
  3973.        ScanU    = (RightU-LeftU)/width;
  3974.        ScanV    = (RightV-LeftV)/width;            
  3975.        MiddleDa = (RightA-LeftA)/width;          
  3976.  
  3977.              _Y1 = _MinClipY;
  3978.     }
  3979.                 
  3980.     if (_Y3 > _MaxClipY)
  3981.       _Y3 = _MaxClipY;
  3982.               
  3983.     Buffer+=(_Y1<<8)+(_Y1<<6);
  3984.  
  3985.     if (_X1>=_MinClipX && _X1<=_MaxClipX &&
  3986.         _X2>=_MinClipX && _X2<=_MaxClipX &&
  3987.         _X3>=_MinClipX && _X3<=_MaxClipX)
  3988.     {                   
  3989.        for (y=_Y1;y<_Y3;y++,Buffer+=SCREENWIDTH)    
  3990.        {        
  3991.           u=LeftU;
  3992.           v=LeftV;
  3993.           MiddleA=LeftA;
  3994.           for (x=( ClipLeftX );x<=( ClipRightX );x++)
  3995.           {                 
  3996.              Buffer[x]=TransTBLptr[ ( LookPalPhong[ TextureMap[ (u>>16)+( (v>>16)<<8 ) ] + PhongTBL[ (MiddleA>>16) ] ]<<8 ) + Buffer[x] ];
  3997.              u+=ScanU;
  3998.              v+=ScanV;
  3999.              MiddleA+=MiddleDa;
  4000.           }
  4001.           LeftX   += LeftDx;
  4002.           RightX  += RightDx;
  4003.           LeftU   += LeftDu;
  4004.           LeftV   += LeftDv;
  4005.           RightU  += RightDu;
  4006.           RightV  += RightDv;
  4007.           LeftA   += LeftDa;      // update the intensities and slopes.
  4008.           RightA  += RightDa;
  4009.  
  4010.           ClipLeftX  = ( LeftX>>16 );
  4011.           ClipRightX = ( RightX>>16 );
  4012.           width      = 1+ClipRightX-ClipLeftX;
  4013.     
  4014.           ScanU    = (RightU-LeftU)/width;
  4015.           ScanV    = (RightV-LeftV)/width;            
  4016.           MiddleDa = (RightA-LeftA)/width;          
  4017.        }
  4018.     }
  4019.     else
  4020.     {
  4021.        for (y=_Y1;y<_Y3;y++,Buffer+=SCREENWIDTH)
  4022.        {
  4023.            
  4024.           u=LeftU;
  4025.           v=LeftV;
  4026.           MiddleA=LeftA;
  4027.  
  4028.           if (ClipLeftX < _MinClipX)
  4029.           {
  4030.             if (ClipRightX <= _MinClipX)
  4031.             {
  4032.                LeftX  += LeftDx;      // update the intensities and slopes.
  4033.                RightX += RightDx;     // before continuing.
  4034.                LeftU  += LeftDu;
  4035.                LeftV  += LeftDv;
  4036.                RightU += RightDu;
  4037.                RightV += RightDv;
  4038.                LeftA  += LeftDa;      
  4039.                RightA += RightDa;         
  4040.  
  4041.                ClipLeftX  = ( LeftX>>16 );
  4042.                ClipRightX = ( RightX>>16 );
  4043.  
  4044.                continue;
  4045.             }
  4046.             u          = LeftU + (_MinClipX - ClipLeftX)*ScanU;
  4047.             v          = LeftV + (_MinClipX - ClipLeftX)*ScanV;
  4048.             MiddleA    = LeftA + (_MinClipX - ClipLeftX)*MiddleDa;   // move the intensity by
  4049.             ClipLeftX  = _MinClipX;   // the clipped amount.
  4050.           }
  4051.  
  4052.           if (ClipRightX > _MaxClipX)
  4053.           {
  4054.             if (ClipLeftX > _MaxClipX)
  4055.             {
  4056.                LeftX  += LeftDx;      // update the intensities and slopes.
  4057.                RightX += RightDx;     // before continuing.
  4058.                LeftU  += LeftDu;
  4059.                LeftV  += LeftDv;
  4060.                RightU += RightDu;
  4061.                RightV += RightDv;
  4062.                LeftA  += LeftDa;      
  4063.                RightA += RightDa;
  4064.  
  4065.                ClipLeftX  = ( LeftX>>16 );
  4066.                ClipRightX = ( RightX>>16 );
  4067.  
  4068.                continue;
  4069.             }
  4070.             ClipRightX = _MaxClipX;
  4071.           }
  4072.                                             
  4073.           for (x=ClipLeftX;x<ClipRightX;x++)
  4074.           {
  4075.              Buffer[x]=TransTBLptr[ ( LookPalPhong[ TextureMap[ (u>>16)+( (v>>16)<<8 ) ] + PhongTBL[ (MiddleA>>16) ] ]<<8 ) + Buffer[x] ];
  4076.              u+=ScanU;
  4077.              v+=ScanV;
  4078.              MiddleA+=MiddleDa;
  4079.           }               
  4080.           LeftX   += LeftDx;
  4081.           RightX  += RightDx;
  4082.           LeftU   += LeftDu;
  4083.           LeftV   += LeftDv;
  4084.           RightU  += RightDu;
  4085.           RightV  += RightDv;
  4086.           LeftA   += LeftDa;     
  4087.           RightA  += RightDa;         
  4088.  
  4089.           ClipLeftX  = ( LeftX>>16 );
  4090.           ClipRightX = ( RightX>>16 );
  4091.           width      = 1+ClipRightX-ClipLeftX;
  4092.     
  4093.           ScanU    = (RightU-LeftU)/width;
  4094.           ScanV    = (RightV-LeftV)/width;            
  4095.           MiddleDa = (RightA-LeftA)/width;          
  4096.        }
  4097.     }
  4098. }
  4099.  
  4100. void Scan_Convert_LambertH(void)
  4101. {
  4102.   long LeftX, RightX, LeftDx, RightDx;
  4103.   long height,slope;
  4104.   long hazevalue,hazeintensity;
  4105.   int start,end,y,newx,tempx,tempy,old_X3,old_Y3,ClipLeftX,ClipRightX;
  4106.   int GENERAL;
  4107.   unsigned char color;
  4108.   unsigned char *Buffer;
  4109.  
  4110.     if ( (_X1==_X2 && _X2==_X3) || (_Y1==_Y2 && _Y2==_Y3) )
  4111.       return;               // degenerated into lines.
  4112.  
  4113.     if (_Y2<_Y1)              // switch. They're in the wrong order.
  4114.     {
  4115.         tempx=_X1; tempy=_Y1;
  4116.         _X1=_X2;    _Y1=_Y2;
  4117.         _X2=tempx; _Y2=tempy;
  4118.     }    
  4119.     if (_Y3<_Y1)              // switch. They're in the wrong order.
  4120.     {
  4121.         tempx=_X1; tempy=_Y1;
  4122.         _X1=_X3;    _Y1=_Y3;
  4123.         _X3=tempx; _Y3=tempy;
  4124.     }
  4125.     if (_Y3<_Y2)              // switch. They're in the wrong order.
  4126.     {
  4127.         tempx=_X2; tempy=_Y2;
  4128.         _X2=_X3;    _Y2=_Y3;
  4129.         _X3=tempx; _Y3=tempy;
  4130.     }
  4131.     
  4132.     if (_Y3<_MinClipY || _Y1>_MaxClipY ||
  4133.        (_X1<_MinClipX && _X2<_MinClipX && _X3<_MinClipX) ||
  4134.        (_X1>_MaxClipX && _X2>_MaxClipX && _X3>_MaxClipX) )
  4135.        return;              // do trivial rejection.
  4136.         
  4137.                             // now ALL vertices are in correct Counter-Clockwise order.
  4138.  
  4139.     GENERAL=FALSE;          // reset cases (for Triangle).
  4140.     Buffer=_RendBuffer;     // save starting point of Buffer.
  4141.  
  4142.     hazevalue=( _AvgZ<<16)/1000;
  4143.     hazeintensity=(64*hazevalue)>>16;
  4144.      
  4145.     color=HazeTBL[ (LookPal[ _ColorIndex ]<<6) + hazeintensity ];
  4146.  
  4147.     if (_Y1==_Y2)
  4148.       goto FLAT_TOP;
  4149.     if (_Y2==_Y3)
  4150.       goto FLAT_BOTTOM;
  4151.     else
  4152.       GENERAL=TRUE;
  4153.          
  4154.     slope = ((_X3-_X1)<<16)/(_Y3-_Y1);               
  4155.     newx  = _X1+( (slope*(_Y2-_Y1))>>16 );
  4156.     old_X3 = _X3;                            // save values for later.
  4157.     old_Y3 = _Y3;
  4158.  
  4159.     _X3    = newx;
  4160.     _Y3    = _Y2;           
  4161.  
  4162.     FLAT_BOTTOM:
  4163.         
  4164.     if (_X3<_X2)
  4165.     {
  4166.       tempx=_X3; _X3=_X2; _X2=tempx;
  4167.     }
  4168.  
  4169.     height  = 65536/(_Y3-_Y1);     // point 16 format.
  4170.     LeftDx  = (_X2-_X1)*height;           // Inverse left and right slope.
  4171.     RightDx = (_X3-_X1)*height;
  4172.  
  4173.     LeftX   = _X1<<16;
  4174.     RightX  = LeftX+32768;              // 32768 is 0.5 in 16 point format.
  4175.  
  4176.     if (_Y1 < _MinClipY)
  4177.     {
  4178.        LeftX  = LeftX+LeftDx*(_MinClipY-_Y1);
  4179.        RightX = RightX+RightDx*(_MinClipY-_Y1);
  4180.            _Y1 = _MinClipY;
  4181.     }
  4182.                 
  4183.     if (_Y3 > _MaxClipY)
  4184.       _Y3 = _MaxClipY;
  4185.               
  4186.     Buffer+=(_Y1<<8)+(_Y1<<6);
  4187.  
  4188.     if (_X1>=_MinClipX && _X1<=_MaxClipX &&
  4189.         _X2>=_MinClipX && _X2<=_MaxClipX &&
  4190.         _X3>=_MinClipX && _X3<=_MaxClipX)
  4191.     {                   
  4192.        for (y=_Y1;y<_Y3;y++,Buffer+=SCREENWIDTH)    
  4193.        {
  4194.           start = LeftX>>16;
  4195.           end   = RightX>>16;
  4196.           Line((unsigned char *)Buffer+start,color,1+end-start);
  4197.           LeftX  += LeftDx;
  4198.           RightX += RightDx;
  4199.        }
  4200.     }
  4201.     else
  4202.     {
  4203.        for (y=_Y1;y<_Y3;y++,Buffer+=SCREENWIDTH)    
  4204.        {
  4205.           ClipLeftX  = ( LeftX>>16 );
  4206.           ClipRightX = ( RightX>>16 );
  4207.  
  4208.           LeftX  += LeftDx;
  4209.           RightX += RightDx;
  4210.  
  4211.           if (ClipLeftX < _MinClipX)
  4212.           {
  4213.             if (ClipRightX <= _MinClipX)
  4214.               continue;
  4215.             ClipLeftX = _MinClipX;
  4216.           }
  4217.  
  4218.           if (ClipRightX > _MaxClipX)
  4219.           {
  4220.             if (ClipLeftX > _MaxClipX)
  4221.               continue;
  4222.             ClipRightX = _MaxClipX;
  4223.           }
  4224.  
  4225.           Line((unsigned char *)Buffer+ClipLeftX,color,1+ClipRightX-ClipLeftX);
  4226.        }
  4227.     }       
  4228.  
  4229.     if (!GENERAL)
  4230.       return;
  4231.  
  4232.     _X1     = _X2;                 // setup for FLAT_TOP.
  4233.     _Y1     = _Y2;
  4234.  
  4235.     _X2     = _X3;
  4236.  
  4237.     _X3     = old_X3;
  4238.     _Y3     = old_Y3;
  4239.  
  4240.     Buffer=_RendBuffer;     // save starting point of Buffer.
  4241.     
  4242.     FLAT_TOP: 
  4243.  
  4244.     if (_X2<_X1)
  4245.     {
  4246.       tempx=_X1; _X1=_X2; _X2=tempx;
  4247.     }
  4248.         
  4249.     height  = 65536/(_Y3-_Y1);
  4250.     LeftDx  = (_X3-_X1)*height;    // Inverse left and right slope.
  4251.     RightDx = (_X3-_X2)*height;
  4252.             
  4253.     LeftX   = _X1<<16;
  4254.     RightX  = (_X2<<16)+32768;          // 32768 is 0.5 in 16 point format.
  4255.  
  4256.     if (_Y1 < _MinClipY)
  4257.     {
  4258.       LeftX  = LeftX+LeftDx*(_MinClipY-_Y1);
  4259.       RightX = RightX+RightDx*(_MinClipY-_Y1);
  4260.           _Y1 = _MinClipY;
  4261.     }
  4262.                         
  4263.     if (_Y3 > _MaxClipY)
  4264.         _Y3 = _MaxClipY;
  4265.            
  4266.     Buffer+=(_Y1<<8)+(_Y1<<6);
  4267.  
  4268.     if (_X1>=_MinClipX && _X1<=_MaxClipX &&
  4269.         _X2>=_MinClipX && _X2<=_MaxClipX &&
  4270.         _X3>=_MinClipX && _X3<=_MaxClipX)
  4271.     {                   
  4272.        for (y=_Y1;y<_Y3;y++,Buffer+=SCREENWIDTH)
  4273.        {           
  4274.           start = LeftX>>16;
  4275.           end   = RightX>>16;
  4276.           Line((unsigned char *)Buffer+start,color,1+end-start);
  4277.           LeftX+=LeftDx;
  4278.           RightX+=RightDx;
  4279.        }
  4280.     }
  4281.     else
  4282.     {
  4283.        for (y=_Y1;y<_Y3;y++,Buffer+=SCREENWIDTH)
  4284.        {
  4285.           ClipLeftX  = ( LeftX>>16 );
  4286.           ClipRightX = ( RightX>>16 );
  4287.               
  4288.           LeftX+=LeftDx;
  4289.           RightX+=RightDx;
  4290.  
  4291.           if (ClipLeftX < _MinClipX)
  4292.           {
  4293.             if (ClipRightX <= _MinClipX)
  4294.               continue;  // totally off screen!
  4295.             ClipLeftX = _MinClipX;
  4296.           }
  4297.  
  4298.           if (ClipRightX > _MaxClipX)
  4299.           {
  4300.             if (ClipLeftX > _MaxClipX)
  4301.               continue;
  4302.             ClipRightX = _MaxClipX;
  4303.           }
  4304.                               
  4305.           Line((unsigned char *)Buffer+ClipLeftX,color,1+ClipRightX-ClipLeftX);
  4306.        }
  4307.     }
  4308. }    
  4309.  
  4310. void Scan_Convert_GouraudH(void)
  4311. {
  4312.   long LeftX, RightX, LeftDx, RightDx;
  4313.   long LeftI,RightI,MiddleI;
  4314.   long LeftDi,RightDi,MiddleDi;
  4315.   long LeftH,RightH,MiddleH;
  4316.   long LeftDh,RightDh,MiddleDh;
  4317.   long slope,height,width,hazevalue;
  4318.   int x,y,newx,newi,newh,tempx,tempy,tempz,tempi,temph;
  4319.   int h1,h2,h3,old_X3,old_Y3,oldh3,old_I3;
  4320.   int ydiff,ClipLeftX,ClipRightX;
  4321.   int GENERAL;
  4322.   unsigned char *Buffer;
  4323.  
  4324.     if ( (_X1==_X2 && _X2==_X3) || (_Y1==_Y2 && _Y2==_Y3) )
  4325.       return;               // degenerated into lines.       
  4326.     
  4327.     if (_Y2<_Y1)              // switch. They're in the wrong order.
  4328.     {
  4329.         tempx=_X1; tempy=_Y1; tempz=_Z1;
  4330.         _X1=_X2;    _Y1=_Y2;    _Z1=_Z2;
  4331.         _X2=tempx; _Y2=tempy; _Z2=tempz;
  4332.  
  4333.         tempi=_I2;            // switch the intensities also.
  4334.         _I2=_I1;
  4335.         _I1=tempi;
  4336.     }    
  4337.     if (_Y3<_Y1)              // switch. They're in the wrong order.
  4338.     {
  4339.         tempx=_X1; tempy=_Y1; tempz=_Z1;
  4340.         _X1=_X3;    _Y1=_Y3;    _Z1=_Z3;
  4341.         _X3=tempx; _Y3=tempy; _Z3=tempz;
  4342.  
  4343.         tempi=_I3;
  4344.         _I3=_I1;
  4345.         _I1=tempi;
  4346.     }
  4347.     if (_Y3<_Y2)              // switch. They're in the wrong order.
  4348.     {
  4349.         tempx=_X2; tempy=_Y2; tempz=_Z2;
  4350.         _X2=_X3;    _Y2=_Y3;    _Z2=_Z3;
  4351.         _X3=tempx; _Y3=tempy; _Z3=tempz;
  4352.  
  4353.         tempi=_I3;
  4354.         _I3=_I2;
  4355.         _I2=tempi;
  4356.     }
  4357.  
  4358.     if (_Y3<_MinClipY || _Y1>_MaxClipY ||
  4359.        (_X1<_MinClipX && _X2<_MinClipX && _X3<_MinClipX) ||
  4360.        (_X1>_MaxClipX && _X2>_MaxClipX && _X3>_MaxClipX) )
  4361.        return;              // do trivial rejection.
  4362.         
  4363.                             // now ALL vertices are in correct Counter-Clockwise order.
  4364.  
  4365.     GENERAL=FALSE;          // reset cases (for Triangle).
  4366.     Buffer=_RendBuffer;     // save starting point of Buffer.
  4367.       
  4368.     hazevalue=(_Z1<<16)/1000;
  4369.     h1=(64*hazevalue)>>16;
  4370.     hazevalue=(_Z2<<16)/1000;
  4371.     h2=(64*hazevalue)>>16;
  4372.     hazevalue=(_Z3<<16)/1000;
  4373.     h3=(64*hazevalue)>>16;
  4374.  
  4375.     if (_Y1==_Y2)
  4376.       goto FLAT_TOP;
  4377.     if (_Y2==_Y3)
  4378.       goto FLAT_BOTTOM;
  4379.     else
  4380.       GENERAL=TRUE;
  4381.          
  4382.     height   = 65536/(_Y3-_Y1);
  4383.     newi     = (((_Y2-_Y1)*_I3+(_Y3-_Y2)*_I1)*height)>>16;
  4384.     slope    = (_X3-_X1)*height;               
  4385.     newx     = _X1+((slope*(_Y2-_Y1))>>16);
  4386.     newh     = (((_Y2-_Y1)*h3+(_Y3-_Y2)*h1)*height)>>16;
  4387.                   
  4388.     old_X3    = _X3;                            // save values for later.
  4389.     old_Y3    = _Y3;
  4390.     oldh3    = h3;    
  4391.     old_I3    = _I3;                        // save int3 for bottom half.
  4392.  
  4393.     _X3       = newx;
  4394.     _Y3       = _Y2;
  4395.     h3       = newh;    
  4396.     _I3       = newi;
  4397.        
  4398.     FLAT_BOTTOM:
  4399.  
  4400.     if (_X3<_X2)
  4401.     {
  4402.       tempx=_X3;  _X3=_X2; _X2=tempx;
  4403.       temph=h3;  h3=h2; h2=temph;
  4404.       tempi=_I3;  _I3=_I2; _I2=tempi;
  4405.     }
  4406.  
  4407.     height   = 65536/(_Y3-_Y1);
  4408.     LeftDx   = (_X2-_X1)*height;    // Inverse left and right slope.
  4409.     RightDx  = (_X3-_X1)*height;
  4410.  
  4411.     LeftX    = _X1<<16;              // assign Buffering coords.
  4412.     RightX   = LeftX+32768;
  4413.  
  4414.     LeftI    = _I1<<16;              // assign intensity to left and right side.
  4415.     RightI   = LeftI;
  4416.     MiddleI  = LeftI;        // Buffering from left intensity.
  4417.  
  4418.     LeftDi   = (_I2 - _I1)*height;  // compute intensity deltas.
  4419.     RightDi  = (_I3 - _I1)*height; 
  4420.     MiddleDi = 0;
  4421.  
  4422.     LeftH    = h1<<16;              // assign intensity to left and right side.
  4423.     RightH   = LeftH;
  4424.     MiddleH  = LeftH;        // Buffering from left intensity.
  4425.  
  4426.     LeftDh   = (h2 - h1)*height;  // compute intensity deltas.
  4427.     RightDh  = (h3 - h1)*height; 
  4428.     MiddleDh = 0;
  4429.  
  4430.     ClipLeftX  = ( LeftX>>16 );
  4431.     ClipRightX = ( RightX>>16 );
  4432.  
  4433.     if (_Y1 < _MinClipY)
  4434.     {
  4435.        ydiff    = (_MinClipY - _Y1);   
  4436.        LeftX    = LeftX+LeftDx*ydiff;
  4437.        RightX   = RightX+RightDx*ydiff;
  4438.  
  4439.        LeftI    = LeftI+LeftDi*ydiff;
  4440.        RightI   = RightI+RightDi*ydiff;
  4441.        MiddleI  = LeftI;
  4442.  
  4443.        LeftH    = LeftH+LeftDh*ydiff;
  4444.        RightH   = RightH+RightDh*ydiff;
  4445.        MiddleH  = LeftH;
  4446.  
  4447.        ClipLeftX  = ( LeftX>>16 );
  4448.        ClipRightX = ( RightX>>16 );
  4449.        width      = 1+ClipRightX-ClipLeftX;
  4450.  
  4451.        MiddleDi = (RightI - LeftI)/width;
  4452.        MiddleDh = (RightH - LeftH)/width;
  4453.        
  4454.            _Y1      = _MinClipY;
  4455.     }
  4456.                 
  4457.     if (_Y3 > _MaxClipY)
  4458.       _Y3 = _MaxClipY;
  4459.               
  4460.     Buffer+=(_Y1<<8)+(_Y1<<6);
  4461.     
  4462.     if (_X1>=_MinClipX && _X1<=_MaxClipX &&
  4463.         _X2>=_MinClipX && _X2<=_MaxClipX &&
  4464.         _X3>=_MinClipX && _X3<=_MaxClipX)
  4465.     {                   
  4466.        for (y=_Y1;y<_Y3;y++,Buffer+=SCREENWIDTH)
  4467.        {
  4468.           for (x=( ClipLeftX );x<=( ClipRightX );x++)
  4469.           {
  4470.              Buffer[x]=HazeTBL[ (LookPal[ _ColorIndex + ( MiddleI>>16 ) ]<<6) + ( MiddleH>>16 ) ];
  4471.              MiddleI += MiddleDi;
  4472.              MiddleH += MiddleDh;
  4473.           }
  4474.           LeftI   += LeftDi;      // update the intensities and slopes.
  4475.           RightI  += RightDi;
  4476.           MiddleI  = LeftI;
  4477.           LeftH   += LeftDh;      // update the intensities and slopes.
  4478.           RightH  += RightDh;
  4479.           MiddleH  = LeftH;      
  4480.           LeftX   += LeftDx;
  4481.           RightX  += RightDx;
  4482.  
  4483.           ClipLeftX  = ( LeftX>>16 );
  4484.           ClipRightX = ( RightX>>16 );
  4485.           width      = 1+ClipRightX-ClipLeftX;
  4486.  
  4487.           MiddleDi = (RightI - LeftI)/width;
  4488.           MiddleDh = (RightH - LeftH)/width;
  4489.        }
  4490.     }
  4491.     else
  4492.     {
  4493.        for (y=_Y1;y<_Y3;y++,Buffer+=SCREENWIDTH)
  4494.        {
  4495.  
  4496.           if (ClipLeftX < _MinClipX)
  4497.           {
  4498.             if (ClipRightX <= _MinClipX)
  4499.             {
  4500.                LeftX   += LeftDx;      // update the intensities and slopes.
  4501.                RightX  += RightDx;     // before continuing.
  4502.                LeftI   += LeftDi;      
  4503.                RightI  += RightDi;
  4504.                LeftH   += LeftDh;      // update the intensities and slopes.
  4505.                RightH  += RightDh;
  4506.  
  4507.                ClipLeftX  = ( LeftX>>16 );
  4508.                ClipRightX = ( RightX>>16 );
  4509.  
  4510.                continue;
  4511.             }
  4512.             MiddleI = LeftI + (_MinClipX - ClipLeftX)*MiddleDi;   // move the intensity by
  4513.             MiddleH = LeftH + (_MinClipX - ClipLeftX)*MiddleDh;   // move the intensity by  
  4514.             ClipLeftX = _MinClipX;                                // the clipped amount.
  4515.           }
  4516.  
  4517.           if (ClipRightX > _MaxClipX)
  4518.           {
  4519.             if (ClipLeftX > _MaxClipX)
  4520.             {
  4521.                LeftX   += LeftDx;      // update the intensities and slopes.
  4522.                RightX  += RightDx;     // before continuing.
  4523.                LeftI   += LeftDi;      
  4524.                RightI  += RightDi;         
  4525.                LeftH   += LeftDh;      // update the intensities and slopes.
  4526.                RightH  += RightDh;
  4527.  
  4528.                ClipLeftX  = ( LeftX>>16 );
  4529.                ClipRightX = ( RightX>>16 );
  4530.  
  4531.                continue;
  4532.             }
  4533.             ClipRightX = _MaxClipX;
  4534.           }
  4535.                       
  4536.           for (x=ClipLeftX;x<=ClipRightX;x++)
  4537.           {
  4538.              Buffer[x]=HazeTBL[ (LookPal[ _ColorIndex + ( MiddleI>>16 ) ]<<6) + ( MiddleH>>16 ) ];
  4539.              MiddleI += MiddleDi;
  4540.              MiddleH += MiddleDh;
  4541.           }
  4542.           LeftI   += LeftDi;      // update the intensities and slopes.
  4543.           RightI  += RightDi;
  4544.           MiddleI  = LeftI;
  4545.           LeftH   += LeftDh;      // update the intensities and slopes.
  4546.           RightH  += RightDh;
  4547.           MiddleH  = LeftH;
  4548.           LeftX   += LeftDx;
  4549.           RightX  += RightDx;
  4550.  
  4551.           ClipLeftX  = ( LeftX>>16 );
  4552.           ClipRightX = ( RightX>>16 );
  4553.           width      = 1+ClipRightX-ClipLeftX;
  4554.  
  4555.           MiddleDi = (RightI - LeftI)/width;
  4556.           MiddleDh = (RightH - LeftH)/width;
  4557.        }
  4558.     }
  4559.  
  4560.     if (!GENERAL)
  4561.       return;
  4562.  
  4563.     _X1     = _X2;                 // setup for FLAT_TOP.
  4564.     _Y1     = _Y2;
  4565.     _I1     = _I2;
  4566.      h1     = h2;
  4567.  
  4568.     _X2     = _X3;
  4569.     _I2     = _I3;
  4570.      h2     = h3;
  4571.  
  4572.     _X3     = old_X3;
  4573.     _Y3     = old_Y3;
  4574.     _I3     = old_I3;
  4575.      h3     = oldh3;
  4576.     
  4577.     Buffer=_RendBuffer;     // save starting point of Buffer.
  4578.     
  4579.     FLAT_TOP: 
  4580.  
  4581.     if (_X2<_X1)
  4582.     {
  4583.       tempx=_X2; _X2=_X1; _X1=tempx;
  4584.       temph=h2; h2=h1; h1=temph;
  4585.       tempi=_I2; _I2=_I1; _I1=tempi;
  4586.     }
  4587.  
  4588.     height   = 65536/(_Y3-_Y1);
  4589.     LeftDx   = (_X3-_X1)*height;    // Inverse left and right slope.
  4590.     RightDx  = (_X3-_X2)*height;
  4591.  
  4592.     LeftX    = _X1<<16;              // assign Buffering coords.
  4593.     RightX   = (_X2<<16)+32768;
  4594.  
  4595.     LeftI    = _I1<<16;              // assign intensity to left and right side.
  4596.     RightI   = _I2<<16;
  4597.     MiddleI  = LeftI;        // Buffering from left intensity.
  4598.  
  4599.     LeftDi   = (_I3 - _I1)*height;  // compute intensity deltas.
  4600.     RightDi  = (_I3 - _I2)*height; 
  4601.  
  4602.     LeftH    = h1<<16;              // assign intensity to left and right side.
  4603.     RightH   = h2<<16;
  4604.     MiddleH  = LeftH;        // Buffering from left intensity.
  4605.  
  4606.     LeftDh   = (h3 - h1)*height;  // compute intensity deltas.
  4607.     RightDh  = (h3 - h2)*height; 
  4608.  
  4609.     ClipLeftX  = ( LeftX>>16 );
  4610.     ClipRightX = ( RightX>>16 );
  4611.     width      = 1+ClipRightX-ClipLeftX;
  4612.  
  4613.     MiddleDi = (RightI - LeftI)/width;
  4614.     MiddleDh = (RightH - LeftH)/width;
  4615.                          
  4616.     if (_Y1 < _MinClipY)
  4617.     {
  4618.        ydiff    = (_MinClipY - _Y1);
  4619.        LeftX    = LeftX+LeftDx*ydiff;
  4620.        RightX   = RightX+RightDx*ydiff;
  4621.  
  4622.        LeftI    = LeftI+LeftDi*ydiff;
  4623.        RightI   = RightI+RightDi*ydiff;
  4624.        MiddleI  = LeftI;
  4625.  
  4626.        LeftH    = LeftH+LeftDh*ydiff;
  4627.        RightH   = RightH+RightDh*ydiff;
  4628.        MiddleH  = LeftH;
  4629.        
  4630.        ClipLeftX  = ( LeftX>>16 );
  4631.        ClipRightX = ( RightX>>16 );
  4632.        width      = 1+ClipRightX-ClipLeftX;
  4633.  
  4634.        MiddleDi = (RightI - LeftI)/width;
  4635.        MiddleDh = (RightH - LeftH)/width;
  4636.  
  4637.             _Y1 = _MinClipY;
  4638.     }
  4639.                 
  4640.     if (_Y3 > _MaxClipY)
  4641.       _Y3 = _MaxClipY;
  4642.               
  4643.     Buffer+=(_Y1<<8)+(_Y1<<6);
  4644.  
  4645.     if (_X1>=_MinClipX && _X1<=_MaxClipX &&
  4646.         _X2>=_MinClipX && _X2<=_MaxClipX &&
  4647.         _X3>=_MinClipX && _X3<=_MaxClipX)
  4648.     {
  4649.        for (y=_Y1;y<_Y3;y++,Buffer+=SCREENWIDTH)
  4650.        {
  4651.           for (x=( ClipLeftX );x<=( ClipRightX );x++)
  4652.           {
  4653.              Buffer[x]=HazeTBL[ (LookPal[ _ColorIndex + ( MiddleI>>16 ) ]<<6) + ( MiddleH>>16 ) ];
  4654.              MiddleI += MiddleDi;
  4655.              MiddleH += MiddleDh;
  4656.           }               
  4657.         LeftI   += LeftDi;      // update the intensities and slopes.
  4658.         RightI  += RightDi;
  4659.         MiddleI  = LeftI;
  4660.         LeftH   += LeftDh;      // update the intensities and slopes.
  4661.         RightH  += RightDh;
  4662.         MiddleH  = LeftH;      
  4663.         LeftX   += LeftDx;
  4664.         RightX  += RightDx;
  4665.  
  4666.         ClipLeftX  = ( LeftX>>16 );
  4667.         ClipRightX = ( RightX>>16 );
  4668.         width      = 1+ClipRightX-ClipLeftX;
  4669.  
  4670.         MiddleDi = (RightI - LeftI)/width;
  4671.         MiddleDh = (RightH - LeftH)/width;
  4672.        }
  4673.     }
  4674.     else
  4675.     {
  4676.        for (y=_Y1;y<_Y3;y++,Buffer+=SCREENWIDTH)    
  4677.        {
  4678.  
  4679.           if (ClipLeftX < _MinClipX)
  4680.           {
  4681.             if (ClipRightX <= _MinClipX)
  4682.             {
  4683.                LeftX   += LeftDx;      // update the intensities and slopes.
  4684.                RightX  += RightDx;     // before continuing.
  4685.                LeftI   += LeftDi;      
  4686.                RightI  += RightDi;         
  4687.                LeftH   += LeftDh;      // update the intensities and slopes.
  4688.                RightH  += RightDh;
  4689.  
  4690.                ClipLeftX  = ( LeftX>>16 );
  4691.                ClipRightX = ( RightX>>16 );
  4692.  
  4693.                continue;
  4694.             }
  4695.             MiddleI = LeftI + (_MinClipX - ClipLeftX)*MiddleDi;   // move the intensity by
  4696.             MiddleH = LeftH + (_MinClipX - ClipLeftX)*MiddleDh;   // move the intensity by  
  4697.             ClipLeftX = _MinClipX;                                // the clipped amount.
  4698.           }
  4699.  
  4700.           if (ClipRightX > _MaxClipX)
  4701.           {
  4702.             if (ClipLeftX > _MaxClipX)
  4703.             {
  4704.                LeftX   += LeftDx;      // update the intensities and slopes.
  4705.                RightX  += RightDx;     // before continuing.
  4706.                LeftI   += LeftDi;      
  4707.                RightI  += RightDi;         
  4708.                LeftH   += LeftDh;      // update the intensities and slopes.
  4709.                RightH  += RightDh;
  4710.  
  4711.                ClipLeftX  = ( LeftX>>16 );
  4712.                ClipRightX = ( RightX>>16 );
  4713.  
  4714.                continue;
  4715.             }
  4716.             ClipRightX = _MaxClipX;
  4717.           }
  4718.                                             
  4719.           for (x=ClipLeftX;x<=ClipRightX;x++)
  4720.           {
  4721.              Buffer[x]=HazeTBL[ (LookPal[ _ColorIndex + ( MiddleI>>16 ) ]<<6) + ( MiddleH>>16 ) ];
  4722.              MiddleI += MiddleDi;
  4723.              MiddleH += MiddleDh;
  4724.           }               
  4725.         LeftI   += LeftDi;      // update the intensities and slopes.
  4726.         RightI  += RightDi;
  4727.         MiddleI  = LeftI;
  4728.         LeftH   += LeftDh;      // update the intensities and slopes.
  4729.         RightH  += RightDh;
  4730.         MiddleH  = LeftH;      
  4731.         LeftX   += LeftDx;
  4732.         RightX  += RightDx;
  4733.  
  4734.         ClipLeftX  = ( LeftX>>16 );
  4735.         ClipRightX = ( RightX>>16 );
  4736.         width      = 1+ClipRightX-ClipLeftX;
  4737.  
  4738.         MiddleDi = (RightI - LeftI)/width;
  4739.         MiddleDh = (RightH - LeftH)/width;
  4740.        }
  4741.     }           
  4742. }
  4743.  
  4744. void Scan_Convert_PhongH(void)
  4745. {
  4746.   long LeftX, RightX, LeftDx, RightDx;
  4747.   long LeftA,RightA,MiddleA;
  4748.   long LeftDa,RightDa,MiddleDa;
  4749.   long LeftH,RightH,MiddleH;
  4750.   long LeftDh,RightDh,MiddleDh;
  4751.   long slope,height,width,hazevalue;
  4752.   int x,y,newx,newa,newh,tempx,tempy,tempz,tempa,temph;
  4753.   int h1,h2,h3,old_X3,old_Y3,oldh3,old_A3;
  4754.   int ydiff,ClipLeftX,ClipRightX;
  4755.   int GENERAL;
  4756.   unsigned char *Buffer;
  4757.  
  4758.     if ( (_X1==_X2 && _X2==_X3) || (_Y1==_Y2 && _Y2==_Y3) )
  4759.       return;               // degenerated into lines.       
  4760.     
  4761.     if (_Y2<_Y1)              // switch. They're in the wrong order.
  4762.     {
  4763.         tempx=_X1; tempy=_Y1; tempz=_Z1;
  4764.         _X1=_X2;    _Y1=_Y2;    _Z1=_Z2;
  4765.         _X2=tempx; _Y2=tempy; _Z2=tempz;
  4766.  
  4767.         tempa=_A2;            // switch the intensities also.
  4768.         _A2=_A1;
  4769.         _A1=tempa;
  4770.     }    
  4771.     if (_Y3<_Y1)              // switch. They're in the wrong order.
  4772.     {
  4773.         tempx=_X1; tempy=_Y1; tempz=_Z1;
  4774.         _X1=_X3;    _Y1=_Y3;    _Z1=_Z3;
  4775.         _X3=tempx; _Y3=tempy; _Z3=tempz;
  4776.  
  4777.         tempa=_A3;
  4778.         _A3=_A1;
  4779.         _A1=tempa;
  4780.     }
  4781.     if (_Y3<_Y2)              // switch. They're in the wrong order.
  4782.     {
  4783.         tempx=_X2; tempy=_Y2; tempz=_Z2;
  4784.         _X2=_X3;    _Y2=_Y3;    _Z2=_Z3;
  4785.         _X3=tempx; _Y3=tempy; _Z3=tempz;
  4786.  
  4787.         tempa=_A3;
  4788.         _A3=_A2;
  4789.         _A2=tempa;
  4790.     }
  4791.  
  4792.     if (_Y3<_MinClipY || _Y1>_MaxClipY ||
  4793.        (_X1<_MinClipX && _X2<_MinClipX && _X3<_MinClipX) ||
  4794.        (_X1>_MaxClipX && _X2>_MaxClipX && _X3>_MaxClipX) )
  4795.        return;              // do trivial rejection.
  4796.         
  4797.                             // now ALL vertices are in correct Counter-Clockwise order.
  4798.  
  4799.     GENERAL=FALSE;          // reset cases (for Triangle).
  4800.     Buffer=_RendBuffer;     // save starting point of Buffer.
  4801.       
  4802.     hazevalue=(_Z1<<16)/1000;
  4803.     h1=(64*hazevalue)>>16;
  4804.     hazevalue=(_Z2<<16)/1000;
  4805.     h2=(64*hazevalue)>>16;
  4806.     hazevalue=(_Z3<<16)/1000;
  4807.     h3=(64*hazevalue)>>16;
  4808.  
  4809.     if (_Y1==_Y2)
  4810.       goto FLAT_TOP;
  4811.     if (_Y2==_Y3)
  4812.       goto FLAT_BOTTOM;
  4813.     else
  4814.       GENERAL=TRUE;
  4815.          
  4816.     height   = 65536/(_Y3-_Y1);
  4817.     newa     = (((_Y2-_Y1)*_A3+(_Y3-_Y2)*_A1)*height)>>16;
  4818.     slope    = (_X3-_X1)*height;               
  4819.     newx     = _X1+((slope*(_Y2-_Y1))>>16);
  4820.     newh     = (((_Y2-_Y1)*h3+(_Y3-_Y2)*h1)*height)>>16;
  4821.                   
  4822.     old_X3    = _X3;                            // save values for later.
  4823.     old_Y3    = _Y3;
  4824.     oldh3    = h3;    
  4825.     old_A3    = _A3;                        // save int3 for bottom half.
  4826.  
  4827.     _X3       = newx;
  4828.     _Y3       = _Y2;
  4829.     h3       = newh;    
  4830.     _A3       = newa;
  4831.        
  4832.     FLAT_BOTTOM:
  4833.  
  4834.     if (_X3<_X2)
  4835.     {
  4836.       tempx=_X3;  _X3=_X2; _X2=tempx;
  4837.       temph=h3;  h3=h2; h2=temph;
  4838.       tempa=_A3;  _A3=_A2; _A2=tempa;
  4839.     }
  4840.  
  4841.     height   = 65536/(_Y3-_Y1);
  4842.     LeftDx   = (_X2-_X1)*height;    // Inverse left and right slope.
  4843.     RightDx  = (_X3-_X1)*height;
  4844.  
  4845.     LeftX    = _X1<<16;              // assign Buffering coords.
  4846.     RightX   = LeftX+32768;
  4847.  
  4848.     LeftA    = _A1<<16;              // assign intensity to left and right side.
  4849.     RightA   = LeftA;
  4850.     MiddleA  = LeftA;        // Buffering from left intensity.
  4851.  
  4852.     LeftDa   = (_A2 - _A1)*height;  // compute intensity deltas.
  4853.     RightDa  = (_A3 - _A1)*height; 
  4854.     MiddleDa = 0;
  4855.  
  4856.     LeftH    = h1<<16;              // assign intensity to left and right side.
  4857.     RightH   = LeftH;
  4858.     MiddleH  = LeftH;        // Buffering from left intensity.
  4859.  
  4860.     LeftDh   = (h2 - h1)*height;  // compute intensity deltas.
  4861.     RightDh  = (h3 - h1)*height; 
  4862.     MiddleDh = 0;
  4863.  
  4864.     ClipLeftX  = ( LeftX>>16 );
  4865.     ClipRightX = ( RightX>>16 );
  4866.  
  4867.     if (_Y1 < _MinClipY)
  4868.     {
  4869.        ydiff    = (_MinClipY - _Y1);   
  4870.        LeftX    = LeftX+LeftDx*ydiff;
  4871.        RightX   = RightX+RightDx*ydiff;
  4872.  
  4873.        LeftA    = LeftA+LeftDa*ydiff;
  4874.        RightA   = RightA+RightDa*ydiff;
  4875.        MiddleA  = LeftA;
  4876.  
  4877.        LeftH    = LeftH+LeftDh*ydiff;
  4878.        RightH   = RightH+RightDh*ydiff;
  4879.        MiddleH  = LeftH;
  4880.  
  4881.        ClipLeftX  = ( LeftX>>16 );
  4882.        ClipRightX = ( RightX>>16 );
  4883.        width      = 1+ClipRightX-ClipLeftX;
  4884.  
  4885.        MiddleDa = (RightA - LeftA)/width;
  4886.        MiddleDh = (RightH - LeftH)/width;
  4887.        
  4888.            _Y1      = _MinClipY;
  4889.     }
  4890.                 
  4891.     if (_Y3 > _MaxClipY)
  4892.       _Y3 = _MaxClipY;
  4893.               
  4894.     Buffer+=(_Y1<<8)+(_Y1<<6);
  4895.     
  4896.     if (_X1>=_MinClipX && _X1<=_MaxClipX &&
  4897.         _X2>=_MinClipX && _X2<=_MaxClipX &&
  4898.         _X3>=_MinClipX && _X3<=_MaxClipX)
  4899.     {                   
  4900.        for (y=_Y1;y<_Y3;y++,Buffer+=SCREENWIDTH)
  4901.        {
  4902.           for (x=( ClipLeftX );x<=( ClipRightX );x++)
  4903.           {
  4904.              Buffer[x]=HazeTBL[ (LookPalPhong[ _ColorIndex + PhongTBL[ ( MiddleA>>16 ) ] ]<<6) + ( MiddleH>>16 ) ];
  4905.              MiddleA += MiddleDa;
  4906.              MiddleH += MiddleDh;
  4907.           }
  4908.           LeftA   += LeftDa;      // update the intensities and slopes.
  4909.           RightA  += RightDa;
  4910.           MiddleA  = LeftA;
  4911.           LeftH   += LeftDh;      // update the intensities and slopes.
  4912.           RightH  += RightDh;
  4913.           MiddleH  = LeftH;      
  4914.           LeftX   += LeftDx;
  4915.           RightX  += RightDx;
  4916.  
  4917.           ClipLeftX  = ( LeftX>>16 );
  4918.           ClipRightX = ( RightX>>16 );
  4919.           width      = 1+ClipRightX-ClipLeftX;
  4920.  
  4921.           MiddleDa = (RightA - LeftA)/width;
  4922.           MiddleDh = (RightH - LeftH)/width;
  4923.        }
  4924.     }
  4925.     else
  4926.     {
  4927.        for (y=_Y1;y<_Y3;y++,Buffer+=SCREENWIDTH)
  4928.        {
  4929.  
  4930.           if (ClipLeftX < _MinClipX)
  4931.           {
  4932.             if (ClipRightX <= _MinClipX)
  4933.             {
  4934.                LeftX   += LeftDx;      // update the intensities and slopes.
  4935.                RightX  += RightDx;     // before continuing.
  4936.                LeftA   += LeftDa;      
  4937.                RightA  += RightDa;
  4938.                LeftH   += LeftDh;      // update the intensities and slopes.
  4939.                RightH  += RightDh;
  4940.  
  4941.                ClipLeftX  = ( LeftX>>16 );
  4942.                ClipRightX = ( RightX>>16 );
  4943.  
  4944.                continue;
  4945.             }
  4946.             MiddleA = LeftA + (_MinClipX - ClipLeftX)*MiddleDa;   // move the intensity by
  4947.             MiddleH = LeftH + (_MinClipX - ClipLeftX)*MiddleDh;   // move the intensity by  
  4948.             ClipLeftX = _MinClipX;                                // the clipped amount.
  4949.           }
  4950.  
  4951.           if (ClipRightX > _MaxClipX)
  4952.           {
  4953.             if (ClipLeftX > _MaxClipX)
  4954.             {
  4955.                LeftX   += LeftDx;      // update the intensities and slopes.
  4956.                RightX  += RightDx;     // before continuing.
  4957.                LeftA   += LeftDa;      
  4958.                RightA  += RightDa;         
  4959.                LeftH   += LeftDh;      // update the intensities and slopes.
  4960.                RightH  += RightDh;
  4961.  
  4962.                ClipLeftX  = ( LeftX>>16 );
  4963.                ClipRightX = ( RightX>>16 );
  4964.  
  4965.                continue;
  4966.             }
  4967.             ClipRightX = _MaxClipX;
  4968.           }
  4969.                       
  4970.           for (x=ClipLeftX;x<=ClipRightX;x++)
  4971.           {
  4972.              Buffer[x]=HazeTBL[ (LookPalPhong[ _ColorIndex + PhongTBL[ ( MiddleA>>16 ) ] ]<<6) + ( MiddleH>>16 ) ];
  4973.              MiddleA += MiddleDa;
  4974.              MiddleH += MiddleDh;
  4975.           }
  4976.           LeftA   += LeftDa;      // update the intensities and slopes.
  4977.           RightA  += RightDa;
  4978.           MiddleA  = LeftA;
  4979.           LeftH   += LeftDh;      // update the intensities and slopes.
  4980.           RightH  += RightDh;
  4981.           MiddleH  = LeftH;
  4982.           LeftX   += LeftDx;
  4983.           RightX  += RightDx;
  4984.  
  4985.           ClipLeftX  = ( LeftX>>16 );
  4986.           ClipRightX = ( RightX>>16 );
  4987.           width      = 1+ClipRightX-ClipLeftX;
  4988.  
  4989.           MiddleDa = (RightA - LeftA)/width;
  4990.           MiddleDh = (RightH - LeftH)/width;
  4991.        }
  4992.     }
  4993.  
  4994.     if (!GENERAL)
  4995.       return;
  4996.  
  4997.     _X1     = _X2;                 // setup for FLAT_TOP.
  4998.     _Y1     = _Y2;
  4999.     _A1     = _A2;
  5000.     h1     = h2;
  5001.  
  5002.     _X2     = _X3;
  5003.     _A2     = _A3;
  5004.     h2     = h3;
  5005.  
  5006.     _X3     = old_X3;
  5007.     _Y3     = old_Y3;
  5008.     _A3     = old_A3;
  5009.     h3     = oldh3;
  5010.     
  5011.     Buffer=_RendBuffer;     // save starting point of Buffer.
  5012.     
  5013.     FLAT_TOP: 
  5014.  
  5015.     if (_X2<_X1)
  5016.     {
  5017.       tempx=_X2; _X2=_X1; _X1=tempx;
  5018.       temph=h2; h2=h1; h1=temph;
  5019.       tempa=_A2; _A2=_A1; _A1=tempa;
  5020.     }
  5021.  
  5022.     height   = 65536/(_Y3-_Y1);
  5023.     LeftDx   = (_X3-_X1)*height;    // Inverse left and right slope.
  5024.     RightDx  = (_X3-_X2)*height;
  5025.  
  5026.     LeftX    = _X1<<16;              // assign Buffering coords.
  5027.     RightX   = (_X2<<16)+32768;
  5028.  
  5029.     LeftA    = _A1<<16;              // assign intensity to left and right side.
  5030.     RightA   = _A2<<16;
  5031.     MiddleA  = LeftA;        // Buffering from left intensity.
  5032.  
  5033.     LeftDa   = (_A3 - _A1)*height;  // compute intensity deltas.
  5034.     RightDa  = (_A3 - _A2)*height; 
  5035.  
  5036.     LeftH    = h1<<16;              // assign intensity to left and right side.
  5037.     RightH   = h2<<16;
  5038.     MiddleH  = LeftH;        // Buffering from left intensity.
  5039.  
  5040.     LeftDh   = (h3 - h1)*height;  // compute intensity deltas.
  5041.     RightDh  = (h3 - h2)*height; 
  5042.  
  5043.     ClipLeftX  = ( LeftX>>16 );
  5044.     ClipRightX = ( RightX>>16 );
  5045.     width      = 1+ClipRightX-ClipLeftX;
  5046.  
  5047.     MiddleDa = (RightA - LeftA)/width;
  5048.     MiddleDh = (RightH - LeftH)/width;
  5049.                          
  5050.     if (_Y1 < _MinClipY)
  5051.     {
  5052.        ydiff    = (_MinClipY - _Y1);
  5053.        LeftX    = LeftX+LeftDx*ydiff;
  5054.        RightX   = RightX+RightDx*ydiff;
  5055.  
  5056.        LeftA    = LeftA+LeftDa*ydiff;
  5057.        RightA   = RightA+RightDa*ydiff;
  5058.        MiddleA  = LeftA;
  5059.  
  5060.        LeftH    = LeftH+LeftDh*ydiff;
  5061.        RightH   = RightH+RightDh*ydiff;
  5062.        MiddleH  = LeftH;
  5063.        
  5064.        ClipLeftX  = ( LeftX>>16 );
  5065.        ClipRightX = ( RightX>>16 );
  5066.        width      = 1+ClipRightX-ClipLeftX;
  5067.  
  5068.        MiddleDa = (RightA - LeftA)/width;
  5069.        MiddleDh = (RightH - LeftH)/width;
  5070.  
  5071.             _Y1 = _MinClipY;
  5072.     }
  5073.                 
  5074.     if (_Y3 > _MaxClipY)
  5075.       _Y3 = _MaxClipY;
  5076.               
  5077.     Buffer+=(_Y1<<8)+(_Y1<<6);
  5078.  
  5079.     if (_X1>=_MinClipX && _X1<=_MaxClipX &&
  5080.         _X2>=_MinClipX && _X2<=_MaxClipX &&
  5081.         _X3>=_MinClipX && _X3<=_MaxClipX)
  5082.     {                   
  5083.        for (y=_Y1;y<_Y3;y++,Buffer+=SCREENWIDTH)
  5084.        {
  5085.           for (x=( ClipLeftX );x<=( ClipRightX );x++)
  5086.           {
  5087.              Buffer[x]=HazeTBL[ (LookPalPhong[ _ColorIndex + PhongTBL[ ( MiddleA>>16 ) ] ]<<6) + ( MiddleH>>16 ) ];
  5088.              MiddleA += MiddleDa;
  5089.              MiddleH += MiddleDh;
  5090.           }               
  5091.         LeftA   += LeftDa;      // update the intensities and slopes.
  5092.         RightA  += RightDa;
  5093.         MiddleA  = LeftA;
  5094.         LeftH   += LeftDh;      // update the intensities and slopes.
  5095.         RightH  += RightDh;
  5096.         MiddleH  = LeftH;      
  5097.         LeftX   += LeftDx;
  5098.         RightX  += RightDx;
  5099.  
  5100.         ClipLeftX  = ( LeftX>>16 );
  5101.         ClipRightX = ( RightX>>16 );
  5102.         width      = 1+ClipRightX-ClipLeftX;
  5103.  
  5104.         MiddleDa = (RightA - LeftA)/width;
  5105.         MiddleDh = (RightH - LeftH)/width;
  5106.        }
  5107.     }
  5108.     else
  5109.     {
  5110.        for (y=_Y1;y<_Y3;y++,Buffer+=SCREENWIDTH)    
  5111.        {
  5112.  
  5113.           if (ClipLeftX < _MinClipX)
  5114.           {
  5115.             if (ClipRightX <= _MinClipX)
  5116.             {
  5117.                LeftX   += LeftDx;      // update the intensities and slopes.
  5118.                RightX  += RightDx;     // before continuing.
  5119.                LeftA   += LeftDa;      
  5120.                RightA  += RightDa;         
  5121.                LeftH   += LeftDh;      // update the intensities and slopes.
  5122.                RightH  += RightDh;
  5123.  
  5124.                ClipLeftX  = ( LeftX>>16 );
  5125.                ClipRightX = ( RightX>>16 );
  5126.  
  5127.                continue;
  5128.             }
  5129.             MiddleA = LeftA + (_MinClipX - ClipLeftX)*MiddleDa;   // move the intensity by
  5130.             MiddleH = LeftH + (_MinClipX - ClipLeftX)*MiddleDh;   // move the intensity by  
  5131.             ClipLeftX = _MinClipX;                                // the clipped amount.
  5132.           }
  5133.  
  5134.           if (ClipRightX > _MaxClipX)
  5135.           {
  5136.             if (ClipLeftX > _MaxClipX)
  5137.             {
  5138.                LeftX   += LeftDx;      // update the intensities and slopes.
  5139.                RightX  += RightDx;     // before continuing.
  5140.                LeftA   += LeftDa;      
  5141.                RightA  += RightDa;         
  5142.                LeftH   += LeftDh;      // update the intensities and slopes.
  5143.                RightH  += RightDh;
  5144.  
  5145.                ClipLeftX  = ( LeftX>>16 );
  5146.                ClipRightX = ( RightX>>16 );
  5147.  
  5148.                continue;
  5149.             }
  5150.             ClipRightX = _MaxClipX;
  5151.           }
  5152.                                             
  5153.           for (x=ClipLeftX;x<=ClipRightX;x++)
  5154.           {
  5155.              Buffer[x]=HazeTBL[ (LookPalPhong[ _ColorIndex + PhongTBL[ ( MiddleA>>16 ) ] ]<<6) + ( MiddleH>>16 ) ];
  5156.              MiddleA += MiddleDa;
  5157.              MiddleH += MiddleDh;
  5158.           }               
  5159.         LeftA   += LeftDa;      // update the intensities and slopes.
  5160.         RightA  += RightDa;
  5161.         MiddleA  = LeftA;
  5162.         LeftH   += LeftDh;      // update the intensities and slopes.
  5163.         RightH  += RightDh;
  5164.         MiddleH  = LeftH;      
  5165.         LeftX   += LeftDx;
  5166.         RightX  += RightDx;
  5167.  
  5168.         ClipLeftX  = ( LeftX>>16 );
  5169.         ClipRightX = ( RightX>>16 );
  5170.         width      = 1+ClipRightX-ClipLeftX;
  5171.  
  5172.         MiddleDa = (RightA - LeftA)/width;
  5173.         MiddleDh = (RightH - LeftH)/width;
  5174.        }
  5175.     }           
  5176. }
  5177.  
  5178. void Scan_Convert_TextureLH(void)
  5179. {
  5180.   long LeftX, RightX, LeftDx, RightDx;
  5181.   long u,v,ScanU,ScanV,LeftU,LeftV,RightU,RightV;
  5182.   long LeftDu,LeftDv,RightDu,RightDv;
  5183.   long hazevalue,hazeintensity,width,height,slope;
  5184.   int ClipLeftX,ClipRightX,ydiff;
  5185.   int x,y,newx,newu,newv,tempx,tempy,tempu,tempv;
  5186.   int old_X3,old_Y3,old_U3,old_V3;
  5187.   int GENERAL;
  5188.   unsigned char *Buffer;
  5189.  
  5190.     if ( (_X1==_X2 && _X2==_X3) || (_Y1==_Y2 && _Y2==_Y3) )
  5191.       return;               // degenerated into lines.       
  5192.  
  5193.     if (_Y2<_Y1)              // switch. They're in the wrong order.
  5194.     {
  5195.         tempx=_X1; tempy=_Y1;
  5196.         _X1=_X2;    _Y1=_Y2;
  5197.         _X2=tempx; _Y2=tempy;
  5198.  
  5199.         tempu=_U1; tempv=_V1;
  5200.         _U1=_U2;    _V1=_V2;
  5201.         _U2=tempu; _V2=tempv;
  5202.     }    
  5203.     if (_Y3<_Y1)              // switch. They're in the wrong order.
  5204.     {
  5205.         tempx=_X1; tempy=_Y1;
  5206.         _X1=_X3;    _Y1=_Y3;
  5207.         _X3=tempx; _Y3=tempy;
  5208.  
  5209.         tempu=_U1; tempv=_V1;
  5210.         _U1=_U3;    _V1=_V3;
  5211.         _U3=tempu; _V3=tempv;
  5212.     }
  5213.     if (_Y3<_Y2)              // switch. They're in the wrong order.
  5214.     {
  5215.         tempx=_X2; tempy=_Y2;
  5216.         _X2=_X3;    _Y2=_Y3;
  5217.         _X3=tempx; _Y3=tempy;
  5218.  
  5219.         tempu=_U2; tempv=_V2;
  5220.         _U2=_U3;    _V2=_V3;
  5221.         _U3=tempu; _V3=tempv;
  5222.     }    
  5223.  
  5224.     if (_Y3<_MinClipY || _Y1>_MaxClipY ||
  5225.        (_X1<_MinClipX && _X2<_MinClipX && _X3<_MinClipX) ||
  5226.        (_X1>_MaxClipX && _X2>_MaxClipX && _X3>_MaxClipX) )
  5227.        return;              // do trivial rejection.
  5228.         
  5229.     GENERAL=FALSE;          // reset cases (for Triangle).
  5230.     Buffer=_RendBuffer;     // save starting point of Buffer.
  5231.     
  5232.     hazevalue=( _AvgZ<<16)/1000;
  5233.     hazeintensity=(64*hazevalue)>>16;
  5234.      
  5235.     if (_Y1==_Y2)
  5236.       goto FLAT_TOP;
  5237.     if (_Y2==_Y3)
  5238.       goto FLAT_BOTTOM;
  5239.     else
  5240.       GENERAL=TRUE;
  5241.          
  5242.     height = 65536/(_Y3-_Y1);
  5243.     slope = (_X3-_X1)*height;               
  5244.     newx  = _X1+( (slope*(_Y2-_Y1))>>16 );
  5245.     newu  = ( ((_Y2-_Y1)*_U3+(_Y3-_Y2)*_U1)*height )>>16;
  5246.     newv  = ( ((_Y2-_Y1)*_V3+(_Y3-_Y2)*_V1)*height )>>16;
  5247.  
  5248.     old_X3 = _X3;                            // save values for later.
  5249.     old_Y3 = _Y3;
  5250.     old_U3 = _U3;
  5251.     old_V3 = _V3;
  5252.     
  5253.     _X3    = newx;
  5254.     _Y3    = _Y2;           
  5255.     _U3    = newu;
  5256.     _V3    = newv;
  5257.     
  5258.     FLAT_BOTTOM:
  5259.         
  5260.     if (_X3<_X2)
  5261.     {
  5262.       tempx=_X3; _X3=_X2; _X2=tempx;
  5263.       tempu=_U3; _U3=_U2; _U2=tempu;
  5264.       tempv=_V3; _V3=_V2; _V2=tempv;
  5265.     }
  5266.  
  5267.     height  = 65536/(_Y2-_Y1);
  5268.     LeftDx  = (_X2-_X1)*height;    // Inverse left and right slope.
  5269.     RightDx = (_X3-_X1)*height;
  5270.     LeftDu  = (_U2-_U1)*height;
  5271.     LeftDv  = (_V2-_V1)*height;
  5272.     RightDu = (_U3-_U1)*height;
  5273.     RightDv = (_V3-_V1)*height;
  5274.  
  5275.     LeftX   = _X1<<16;
  5276.     RightX  = LeftX+32768;
  5277.     LeftU   = _U1<<16;
  5278.     LeftV   = _V1<<16;
  5279.     RightU  = LeftU;
  5280.     RightV  = LeftV;
  5281.     ScanU  =  0;
  5282.     ScanV  =  0;
  5283.  
  5284.     ClipLeftX  = ( LeftX>>16 );
  5285.     ClipRightX = ( RightX>>16 );
  5286.  
  5287.     if (_Y1 < _MinClipY)
  5288.     {
  5289.        ydiff  = (_MinClipY - _Y1);
  5290.        LeftX  = LeftX+LeftDx*ydiff;
  5291.        RightX = RightX+RightDx*ydiff;
  5292.  
  5293.        LeftU  = LeftU+LeftDu*ydiff;
  5294.        RightU = RightU+RightDu*ydiff;
  5295.        LeftV  = LeftV+LeftDv*ydiff;
  5296.        RightV = RightV+RightDv*ydiff;
  5297.  
  5298.        ClipLeftX  = ( LeftX>>16 );
  5299.        ClipRightX = ( RightX>>16 );
  5300.        width      = 1+ClipRightX-ClipLeftX;
  5301.         
  5302.        ScanU  =  (RightU-LeftU)/width;
  5303.        ScanV  =  (RightV-LeftV)/width;            
  5304.  
  5305.            _Y1 = _MinClipY;
  5306.     }
  5307.                 
  5308.     if (_Y3 > _MaxClipY)
  5309.       _Y3 = _MaxClipY;             
  5310.     Buffer+=(_Y1<<8)+(_Y1<<6);
  5311.  
  5312.     if (_X1>=_MinClipX && _X1<=_MaxClipX &&
  5313.         _X2>=_MinClipX && _X2<=_MaxClipX &&
  5314.         _X3>=_MinClipX && _X3<=_MaxClipX)
  5315.     {                   
  5316.        for (y=_Y1;y<_Y3;y++,Buffer+=SCREENWIDTH)    
  5317.        {
  5318.           u=LeftU;
  5319.           v=LeftV;
  5320.           for (x=( ClipLeftX );x<=( ClipRightX );x++)
  5321.           {                 
  5322.              Buffer[x]=HazeTBL[ (LookPal[ TextureMap[ (u>>16)+( (v>>16)<<8) ] + _ColorIndex ]<<6) + hazeintensity];
  5323.              u+=ScanU;
  5324.              v+=ScanV;
  5325.           }       
  5326.           LeftX  += LeftDx;
  5327.           RightX += RightDx;
  5328.           LeftU  += LeftDu;
  5329.           LeftV  += LeftDv;
  5330.           RightU += RightDu;
  5331.           RightV += RightDv;
  5332.  
  5333.           ClipLeftX  = ( LeftX>>16 );
  5334.           ClipRightX = ( RightX>>16 );
  5335.           width      = 1+ClipRightX-ClipLeftX;
  5336.  
  5337.           ScanU   = (RightU-LeftU)/width;
  5338.           ScanV   = (RightV-LeftV)/width;            
  5339.        }
  5340.     }
  5341.     else
  5342.     {
  5343.        for (y=_Y1;y<_Y3;y++,Buffer+=SCREENWIDTH)    
  5344.        {
  5345.  
  5346.           u=LeftU;
  5347.           v=LeftV;
  5348.  
  5349.           if (ClipLeftX < _MinClipX)
  5350.           {
  5351.             if (ClipRightX <= _MinClipX)
  5352.             {
  5353.                LeftX  += LeftDx;      // update the intensities and slopes.
  5354.                RightX += RightDx;     // before continuing.
  5355.                LeftU  += LeftDu;
  5356.                LeftV  += LeftDv;
  5357.                RightU += RightDu;
  5358.                RightV += RightDv;
  5359.  
  5360.                ClipLeftX  = ( LeftX>>16 );
  5361.                ClipRightX = ( RightX>>16 );
  5362.  
  5363.                continue;
  5364.             }
  5365.             u         = LeftU + (_MinClipX - ClipLeftX)*ScanU;
  5366.             v         = LeftV + (_MinClipX - ClipLeftX)*ScanV;
  5367.             ClipLeftX = _MinClipX;     // the clipped amount.
  5368.           }
  5369.  
  5370.           if (ClipRightX > _MaxClipX)
  5371.           {
  5372.             if (ClipLeftX > _MaxClipX)
  5373.             {
  5374.                LeftX  += LeftDx;      // update the intensities and slopes.
  5375.                RightX += RightDx;     // before continuing.
  5376.                LeftU  += LeftDu;
  5377.                LeftV  += LeftDv;
  5378.                RightU += RightDu;
  5379.                RightV += RightDv;
  5380.  
  5381.                ClipLeftX  = ( LeftX>>16 );
  5382.                ClipRightX = ( RightX>>16 );
  5383.  
  5384.                continue;
  5385.             }
  5386.             ClipRightX = _MaxClipX;
  5387.           }
  5388.                                             
  5389.           for (x=ClipLeftX;x<ClipRightX;x++)
  5390.           {
  5391.              Buffer[x]=HazeTBL[ (LookPal[ TextureMap[ (u>>16)+( (v>>16)<<8) ] + _ColorIndex ]<<6) + hazeintensity];
  5392.              u+=ScanU;
  5393.              v+=ScanV;
  5394.           }               
  5395.           LeftX  += LeftDx;
  5396.           RightX += RightDx;
  5397.           LeftU  += LeftDu;
  5398.           LeftV  += LeftDv;
  5399.           RightU += RightDu;
  5400.           RightV += RightDv;
  5401.  
  5402.           ClipLeftX  = ( LeftX>>16 );
  5403.           ClipRightX = ( RightX>>16 );
  5404.           width      = 1+ClipRightX-ClipLeftX;
  5405.  
  5406.           ScanU   = (RightU-LeftU)/width;
  5407.           ScanV   = (RightV-LeftV)/width;            
  5408.        }
  5409.     }
  5410.  
  5411.     if (!GENERAL)
  5412.       return;
  5413.  
  5414.     _X1     = _X2;                 // setup for FLAT_TOP.
  5415.     _Y1     = _Y2;
  5416.     _U1     = _U2;
  5417.     _V1     = _V2;
  5418.  
  5419.     _X2     = _X3;
  5420.     _U2     = _U3;
  5421.     _V2     = _V3;
  5422.  
  5423.     _X3     = old_X3;
  5424.     _Y3     = old_Y3;
  5425.     _U3     = old_U3;
  5426.     _V3     = old_V3;
  5427.     
  5428.     Buffer=_RendBuffer;     // save starting point of Buffer.
  5429.     
  5430.     FLAT_TOP: 
  5431.  
  5432.     if (_X2<_X1)
  5433.     {
  5434.       tempx=_X2; _X2=_X1; _X1=tempx;
  5435.       tempu=_U2; _U2=_U1; _U1=tempu;
  5436.       tempv=_V2; _V2=_V1; _V1=tempv;
  5437.     }
  5438.  
  5439.     height    = 65536/(_Y3-_Y1);
  5440.  
  5441.     LeftDx    = (_X3-_X1)*height;    // Inverse left and right slope.
  5442.     RightDx   = (_X3-_X2)*height;
  5443.     LeftDu    = (_U3-_U1)*height;
  5444.     LeftDv    = (_V3-_V1)*height;
  5445.     RightDu   = (_U3-_U2)*height;
  5446.     RightDv   = (_V3-_V2)*height;
  5447.  
  5448.     LeftX     = _X1<<16;
  5449.     RightX    = (_X2<<16)+32768;
  5450.     LeftU     = _U1<<16;
  5451.     LeftV     = _V1<<16;
  5452.     RightU    = _U2<<16;
  5453.     RightV    = _V2<<16;
  5454.  
  5455.     ClipLeftX  = ( LeftX>>16 );
  5456.     ClipRightX = ( RightX>>16 );
  5457.     width      = 1+ClipRightX-ClipLeftX;
  5458.  
  5459.     ScanU     = (RightU-LeftU)/width;
  5460.     ScanV     = (RightV-LeftV)/width;            
  5461.                        
  5462.     if (_Y1 < _MinClipY)
  5463.     {
  5464.        ydiff  = (_MinClipY - _Y1);   
  5465.        LeftX  = LeftX+LeftDx*ydiff;
  5466.        RightX = RightX+RightDx*ydiff;
  5467.  
  5468.        LeftU  = LeftU+LeftDu*ydiff;
  5469.        RightU = RightU+RightDu*ydiff;
  5470.        LeftV  = LeftV+LeftDv*ydiff;
  5471.        RightV = RightV+RightDv*ydiff;
  5472.  
  5473.        ClipLeftX  = ( LeftX>>16 );
  5474.        ClipRightX = ( RightX>>16 );
  5475.        width      = 1+ClipRightX-ClipLeftX;
  5476.  
  5477.        ScanU  = (RightU-LeftU)/width;
  5478.        ScanV  = (RightV-LeftV)/width;            
  5479.  
  5480.            _Y1 = _MinClipY;
  5481.     }
  5482.                 
  5483.     if (_Y3 > _MaxClipY)
  5484.       _Y3 = _MaxClipY;
  5485.               
  5486.     Buffer+=(_Y1<<8)+(_Y1<<6);
  5487.  
  5488.     if (_X1>=_MinClipX && _X1<=_MaxClipX &&
  5489.         _X2>=_MinClipX && _X2<=_MaxClipX &&
  5490.         _X3>=_MinClipX && _X3<=_MaxClipX)
  5491.     {                   
  5492.        for (y=_Y1;y<_Y3;y++,Buffer+=SCREENWIDTH)    
  5493.        {        
  5494.           u=LeftU;
  5495.           v=LeftV;
  5496.           for (x=( ClipLeftX );x<=( ClipRightX );x++)
  5497.           {                 
  5498.              Buffer[x]=HazeTBL[ (LookPal[ TextureMap[ (u>>16)+( (v>>16)<<8) ] + _ColorIndex ]<<6) + hazeintensity];
  5499.              u+=ScanU;
  5500.              v+=ScanV;
  5501.           }
  5502.           LeftX  += LeftDx;
  5503.           RightX += RightDx;
  5504.           LeftU  += LeftDu;
  5505.           LeftV  += LeftDv;
  5506.           RightU += RightDu;
  5507.           RightV += RightDv;
  5508.  
  5509.           ClipLeftX  = ( LeftX>>16 );
  5510.           ClipRightX = ( RightX>>16 );
  5511.           width      = 1+ClipRightX-ClipLeftX;
  5512.  
  5513.           ScanU   = (RightU-LeftU)/width;
  5514.           ScanV   = (RightV-LeftV)/width;            
  5515.        }
  5516.     }
  5517.     else
  5518.     {
  5519.        for (y=_Y1;y<_Y3;y++,Buffer+=SCREENWIDTH)    
  5520.        {
  5521.  
  5522.           u=LeftU;
  5523.           v=LeftV;
  5524.  
  5525.           if (ClipLeftX < _MinClipX)
  5526.           {
  5527.             if (ClipRightX <= _MinClipX)
  5528.             {
  5529.                LeftX  += LeftDx;      // update the intensities and slopes.
  5530.                RightX += RightDx;     // before continuing.
  5531.                LeftU  += LeftDu;
  5532.                LeftV  += LeftDv;
  5533.                RightU += RightDu;
  5534.                RightV += RightDv;
  5535.  
  5536.                ClipLeftX  = ( LeftX>>16 );
  5537.                ClipRightX = ( RightX>>16 );
  5538.  
  5539.                continue;
  5540.             }
  5541.             u          = LeftU + (_MinClipX - ClipLeftX)*ScanU;
  5542.             v          = LeftV + (_MinClipX - ClipLeftX)*ScanV;
  5543.             ClipLeftX  = _MinClipX;   // the clipped amount.
  5544.           }
  5545.  
  5546.           if (ClipRightX > _MaxClipX)
  5547.           {
  5548.             if (ClipLeftX > _MaxClipX)
  5549.             {
  5550.                LeftX  += LeftDx;      // update the intensities and slopes.
  5551.                RightX += RightDx;     // before continuing.
  5552.                LeftU  += LeftDu;
  5553.                LeftV  += LeftDv;
  5554.                RightU += RightDu;
  5555.                RightV += RightDv;
  5556.  
  5557.                ClipLeftX  = ( LeftX>>16 );
  5558.                ClipRightX = ( RightX>>16 );
  5559.  
  5560.                continue;
  5561.             }
  5562.             ClipRightX = _MaxClipX;
  5563.           }
  5564.                                             
  5565.           for (x=ClipLeftX;x<ClipRightX;x++)
  5566.           {
  5567.              Buffer[x]=HazeTBL[ (LookPal[ TextureMap[ (u>>16)+( (v>>16)<<8) ] + _ColorIndex ]<<6) + hazeintensity];
  5568.              u+=ScanU;
  5569.              v+=ScanV;
  5570.           }               
  5571.           LeftX  += LeftDx;
  5572.           RightX += RightDx;
  5573.           LeftU  += LeftDu;
  5574.           LeftV  += LeftDv;
  5575.           RightU += RightDu;
  5576.           RightV += RightDv;
  5577.  
  5578.           ClipLeftX  = ( LeftX>>16 );
  5579.           ClipRightX = ( RightX>>16 );
  5580.           width      = 1+ClipRightX-ClipLeftX;
  5581.  
  5582.           ScanU   = (RightU-LeftU)/width;
  5583.           ScanV   = (RightV-LeftV)/width;            
  5584.        }
  5585.     }
  5586. }
  5587.  
  5588. void Scan_Convert_TextureGH(void)
  5589. {
  5590.   long LeftX, RightX, LeftDx, RightDx;
  5591.   long u,v,ScanU,ScanV,LeftU,LeftV,RightU,RightV,LeftI,RightI,MiddleI;
  5592.   long LeftDu,LeftDv,RightDu,RightDv,LeftDi,RightDi,MiddleDi;
  5593.   long hazevalue,height,width,slope,ydiff;
  5594.   long LeftH,RightH,MiddleH;
  5595.   long LeftDh,RightDh,MiddleDh;
  5596.   int h1,h2,h3,newh,ClipLeftX,ClipRightX;
  5597.   int x,y,newi,newx,newu,newv,tempx,tempy,tempu,tempv,tempi,temph;
  5598.   int old_X3,old_Y3,old_U3,old_V3,old_I3,oldh3;
  5599.   int GENERAL;
  5600.   unsigned char *Buffer;
  5601.   
  5602.     if ( (_X1==_X2 && _X2==_X3) || (_Y1==_Y2 && _Y2==_Y3) )
  5603.       return;               // degenerated into lines.       
  5604.  
  5605.     if (_Y2<_Y1)              // switch. They're in the wrong order.
  5606.     {
  5607.         tempx=_X1; tempy=_Y1;
  5608.         _X1=_X2;    _Y1=_Y2;
  5609.         _X2=tempx; _Y2=tempy;
  5610.  
  5611.         tempu=_U1; tempv=_V1; tempi=_I1;
  5612.         _U1=_U2;    _V1=_V2;    _I1=_I2;
  5613.         _U2=tempu; _V2=tempv; _I2=tempi;
  5614.     }    
  5615.     if (_Y3<_Y1)              // switch. They're in the wrong order.
  5616.     {
  5617.         tempx=_X1; tempy=_Y1;
  5618.         _X1=_X3;    _Y1=_Y3;
  5619.         _X3=tempx; _Y3=tempy;
  5620.  
  5621.         tempu=_U1; tempv=_V1; tempi=_I1;
  5622.         _U1=_U3;    _V1=_V3;    _I1=_I3;
  5623.         _U3=tempu; _V3=tempv; _I3=tempi;
  5624.     }
  5625.     if (_Y3<_Y2)              // switch. They're in the wrong order.
  5626.     {
  5627.         tempx=_X2; tempy=_Y2;
  5628.         _X2=_X3;    _Y2=_Y3;
  5629.         _X3=tempx; _Y3=tempy;
  5630.  
  5631.         tempu=_U2; tempv=_V2; tempi=_I2;
  5632.         _U2=_U3;    _V2=_V3;    _I2=_I3;
  5633.         _U3=tempu; _V3=tempv; _I3=tempi;
  5634.     }    
  5635.  
  5636.     if (_Y3<_MinClipY || _Y1>_MaxClipY ||
  5637.        (_X1<_MinClipX && _X2<_MinClipX && _X3<_MinClipX) ||
  5638.        (_X1>_MaxClipX && _X2>_MaxClipX && _X3>_MaxClipX) )
  5639.        return;              // do trivial rejection.
  5640.         
  5641.     GENERAL=FALSE;          // reset cases (for Triangle).
  5642.     Buffer=_RendBuffer;     // save starting point of Buffer.
  5643.     
  5644.     hazevalue=(_Z1<<16)/1000;
  5645.     h1=(64*hazevalue)>>16;
  5646.     hazevalue=(_Z2<<16)/1000;
  5647.     h2=(64*hazevalue)>>16;
  5648.     hazevalue=(_Z3<<16)/1000;
  5649.     h3=(64*hazevalue)>>16;
  5650.  
  5651.     if (_Y1==_Y2)
  5652.       goto FLAT_TOP;
  5653.     if (_Y2==_Y3)
  5654.       goto FLAT_BOTTOM;
  5655.     else
  5656.       GENERAL=TRUE;
  5657.          
  5658.     height  = 65536/(_Y3-_Y1);
  5659.     slope   = (_X3-_X1)*height;               
  5660.     newx    = _X1+( (slope*(_Y2-_Y1))>>16 );
  5661.     newu    = (((_Y2-_Y1)*_U3+(_Y3-_Y2)*_U1)*height)>>16;
  5662.     newv    = (((_Y2-_Y1)*_V3+(_Y3-_Y2)*_V1)*height)>>16;
  5663.     newi    = (((_Y2-_Y1)*_I3+(_Y3-_Y2)*_I1)*height)>>16;
  5664.     newh    = (((_Y2-_Y1)*h3+(_Y3-_Y2)*h1)*height)>>16;
  5665.  
  5666.     old_X3    = _X3;                            // save values for later.
  5667.     old_Y3    = _Y3;
  5668.     old_U3    = _U3;
  5669.     old_V3    = _V3;
  5670.     old_I3    = _I3;       
  5671.     oldh3    = h3;    
  5672.     
  5673.     _X3      = newx;
  5674.     _Y3      = _Y2;           
  5675.     _U3      = newu;
  5676.     _V3      = newv;
  5677.     _I3      = newi;
  5678.     h3      = newh;
  5679.     
  5680.     FLAT_BOTTOM:
  5681.  
  5682.     if (_X3<_X2)
  5683.     {
  5684.       tempx=_X3; _X3=_X2; _X2=tempx;
  5685.       tempu=_U3; _U3=_U2; _U2=tempu;
  5686.       tempv=_V3; _V3=_V2; _V2=tempv;
  5687.       tempi=_I3; _I3=_I2; _I2=tempi;
  5688.       temph=h3; h3=h2; h2=temph;
  5689.     }
  5690.  
  5691.     height  = 65536/(_Y3-_Y1);
  5692.     LeftDx  = (_X2-_X1)*height;    // Inverse left and right slope.
  5693.     RightDx = (_X3-_X1)*height;
  5694.     LeftDu  = (_U2-_U1)*height;
  5695.     LeftDv  = (_V2-_V1)*height;
  5696.     RightDu = (_U3-_U1)*height;
  5697.     RightDv = (_V3-_V1)*height;
  5698.     LeftDi  = (_I2-_I1)*height;  // compute intensity deltas.
  5699.     RightDi = (_I3-_I1)*height; 
  5700.     LeftDh  = (h2-h1)*height;
  5701.     RightDh = (h3-h1)*height;
  5702.     
  5703.     LeftX   = _X1<<16;
  5704.     RightX  = LeftX+32768;
  5705.     LeftU   = _U1<<16;
  5706.     LeftV   = _V1<<16;
  5707.     RightU  = LeftU;
  5708.     RightV  = LeftV;
  5709.     LeftI   = _I1<<16;              // assign intensity to left and right side.
  5710.     RightI  = LeftI;
  5711.     LeftH   = h1<<16;
  5712.     RightH  = LeftH;
  5713.     
  5714.     ScanU    = 0;              // because of Flat bottom.
  5715.     ScanV    = 0;
  5716.     MiddleDi = 0;
  5717.     MiddleDh = 0;
  5718.     
  5719.     ClipLeftX  = ( LeftX>>16 );
  5720.     ClipRightX = ( RightX>>16 );
  5721.  
  5722.     if (_Y1 < _MinClipY)
  5723.     {
  5724.        ydiff    = (_MinClipY - _Y1);
  5725.        LeftX    = LeftX+LeftDx*ydiff;
  5726.        RightX   = RightX+RightDx*ydiff;
  5727.  
  5728.        LeftU    = LeftU+LeftDu*ydiff;
  5729.        RightU   = RightU+RightDu*ydiff;
  5730.        LeftV    = LeftV+LeftDv*ydiff;
  5731.        RightV   = RightV+RightDv*ydiff;
  5732.  
  5733.        LeftI    = LeftI+LeftDi*ydiff;
  5734.        RightI   = RightI+RightDi*ydiff;
  5735.  
  5736.        LeftH    = LeftH+LeftDh*ydiff;
  5737.        RightH   = RightH+RightDh*ydiff;
  5738.  
  5739.        ClipLeftX  = ( LeftX>>16 );
  5740.        ClipRightX = ( RightX>>16 );
  5741.        width      = 1+ClipRightX-ClipLeftX;
  5742.  
  5743.        ScanU    = (RightU-LeftU)/width;
  5744.        ScanV    = (RightV-LeftV)/width;            
  5745.        MiddleDi = (RightI-LeftI)/width;          
  5746.        MiddleDh = (RightH-LeftH)/width;
  5747.  
  5748.            _Y1   = _MinClipY;
  5749.     }
  5750.                 
  5751.     if (_Y3 > _MaxClipY)
  5752.       _Y3 = _MaxClipY;
  5753.               
  5754.     Buffer+=(_Y1<<8)+(_Y1<<6);
  5755.  
  5756.     if (_X1>=_MinClipX && _X1<=_MaxClipX &&
  5757.         _X2>=_MinClipX && _X2<=_MaxClipX &&
  5758.         _X3>=_MinClipX && _X3<=_MaxClipX)
  5759.     {                   
  5760.        for (y=_Y1;y<_Y3;y++,Buffer+=SCREENWIDTH)    
  5761.        {
  5762.           u=LeftU;
  5763.           v=LeftV;
  5764.           MiddleI=LeftI;
  5765.           MiddleH=LeftH;
  5766.           for (x=( ClipLeftX );x<=( ClipRightX );x++)
  5767.           {                 
  5768.              Buffer[x]=HazeTBL[ (LookPal[ TextureMap[ (u>>16)+( (v>>16)<<8) ] + (MiddleI>>16) ]<<6) + (MiddleH>>16) ];
  5769.              u+=ScanU;
  5770.              v+=ScanV;
  5771.              MiddleI+=MiddleDi;
  5772.              MiddleH+=MiddleDh;
  5773.           }       
  5774.           LeftX   += LeftDx;
  5775.           RightX  += RightDx;
  5776.           LeftU   += LeftDu;
  5777.           LeftV   += LeftDv;
  5778.           RightU  += RightDu;
  5779.           RightV  += RightDv;
  5780.           LeftI   += LeftDi;      // update the intensities and slopes.
  5781.           RightI  += RightDi;
  5782.           LeftH   += LeftDh;
  5783.           RightH  += RightDh;
  5784.  
  5785.           ClipLeftX  = ( LeftX>>16 );
  5786.           ClipRightX = ( RightX>>16 );
  5787.           width      = 1+ClipRightX-ClipLeftX;
  5788.  
  5789.           ScanU    = (RightU-LeftU)/width;
  5790.           ScanV    = (RightV-LeftV)/width;            
  5791.           MiddleDi = (RightI-LeftI)/width;          
  5792.           MiddleDh = (RightH-LeftH)/width;
  5793.        }
  5794.     }
  5795.     else
  5796.     {
  5797.        for (y=_Y1;y<_Y3;y++,Buffer+=SCREENWIDTH)    
  5798.        {
  5799.  
  5800.           u=LeftU;
  5801.           v=LeftV;
  5802.           MiddleI=LeftI;                                            
  5803.           MiddleH=LeftH;
  5804.           
  5805.           if (ClipLeftX < _MinClipX)
  5806.           {
  5807.             if (ClipRightX <= _MinClipX)
  5808.             {
  5809.                LeftX  += LeftDx;      // update the intensities and slopes.
  5810.                RightX += RightDx;     // before continuing.
  5811.                LeftU  += LeftDu;
  5812.                LeftV  += LeftDv;
  5813.                RightU += RightDu;
  5814.                RightV += RightDv;
  5815.                LeftI  += LeftDi;      
  5816.                RightI += RightDi;
  5817.                LeftH  += LeftDh;
  5818.                RightH += RightDh;              
  5819.  
  5820.                ClipLeftX  = ( LeftX>>16 );
  5821.                ClipRightX = ( RightX>>16 );
  5822.  
  5823.                continue;
  5824.             }
  5825.             u         = LeftU + (_MinClipX - ClipLeftX)*ScanU;
  5826.             v         = LeftV + (_MinClipX - ClipLeftX)*ScanV;
  5827.             MiddleI   = LeftI + (_MinClipX - ClipLeftX)*MiddleDi;   // move the intensity by  
  5828.             MiddleH   = LeftH + (_MinClipX - ClipLeftX)*MiddleDh;
  5829.             ClipLeftX = _MinClipX;     // the clipped amount.
  5830.           }
  5831.  
  5832.           if (ClipRightX > _MaxClipX)
  5833.           {
  5834.             if (ClipLeftX > _MaxClipX)
  5835.             {
  5836.                LeftX  += LeftDx;      // update the intensities and slopes.
  5837.                RightX += RightDx;     // before continuing.
  5838.                LeftU  += LeftDu;
  5839.                LeftV  += LeftDv;
  5840.                RightU += RightDu;
  5841.                RightV += RightDv;
  5842.                LeftI  += LeftDi;      
  5843.                RightI += RightDi;
  5844.                LeftH  += LeftDh;
  5845.                RightH += RightDh;       
  5846.  
  5847.                ClipLeftX  = ( LeftX>>16 );
  5848.                ClipRightX = ( RightX>>16 );
  5849.  
  5850.                continue;
  5851.             }
  5852.             ClipRightX = _MaxClipX;
  5853.           }
  5854.           
  5855.           for (x=ClipLeftX;x<ClipRightX;x++)
  5856.           {
  5857.              Buffer[x]=HazeTBL[ (LookPal[ TextureMap[ (u>>16)+( (v>>16)<<8) ] + (MiddleI>>16) ]<<6) + (MiddleH>>16) ];
  5858.              u+=ScanU;
  5859.              v+=ScanV;
  5860.              MiddleI+=MiddleDi;
  5861.              MiddleH+=MiddleDh;
  5862.           }               
  5863.         LeftX   += LeftDx;
  5864.         RightX  += RightDx;
  5865.         LeftU   += LeftDu;
  5866.         LeftV   += LeftDv;
  5867.         RightU  += RightDu;
  5868.         RightV  += RightDv;
  5869.         LeftI   += LeftDi;      // update the intensities and slopes.
  5870.         RightI  += RightDi;
  5871.         LeftH   += LeftDh;
  5872.         RightH  += RightDh;
  5873.  
  5874.         ClipLeftX  = ( LeftX>>16 );
  5875.         ClipRightX = ( RightX>>16 );
  5876.         width      = 1+ClipRightX-ClipLeftX;
  5877.  
  5878.         ScanU    = (RightU-LeftU)/width;
  5879.         ScanV    = (RightV-LeftV)/width;            
  5880.         MiddleDi = (RightI-LeftI)/width;          
  5881.         MiddleDh = (RightH-LeftH)/width;
  5882.        }
  5883.     }
  5884.  
  5885.     if (!GENERAL)
  5886.       return;
  5887.  
  5888.     _X1 = _X2;                 // setup for FLAT_TOP.
  5889.     _Y1 = _Y2;
  5890.     _U1 = _U2;
  5891.     _V1 = _V2;
  5892.     _I1 = _I2;
  5893.     h1 = h2;
  5894.     
  5895.     _X2 = _X3;
  5896.     _U2 = _U3;
  5897.     _V2 = _V3;
  5898.     _I2 = _I3;
  5899.     h2 = h3;
  5900.     
  5901.     _X3 = old_X3;
  5902.     _Y3 = old_Y3;
  5903.     _U3 = old_U3;
  5904.     _V3 = old_V3;
  5905.     _I3 = old_I3;
  5906.     h3 = oldh3;
  5907.     
  5908.     Buffer=_RendBuffer;     // save starting point of Buffer.
  5909.     
  5910.     FLAT_TOP: 
  5911.  
  5912.     if (_X2<_X1)
  5913.     {
  5914.       tempx=_X2; _X2=_X1; _X1=tempx;
  5915.       tempu=_U2; _U2=_U1; _U1=tempu;
  5916.       tempv=_V2; _V2=_V1; _V1=tempv;
  5917.       tempi=_I2; _I2=_I1; _I1=tempi;
  5918.       temph=h2; h2=h1; h1=temph;
  5919.     }
  5920.  
  5921.     height    = 65536/(_Y3-_Y1);
  5922.  
  5923.     LeftDx    = (_X3-_X1)*height;    // Inverse left and right slope.
  5924.     RightDx   = (_X3-_X2)*height;
  5925.     LeftDu    = (_U3-_U1)*height;
  5926.     LeftDv    = (_V3-_V1)*height;
  5927.     RightDu   = (_U3-_U2)*height;
  5928.     RightDv   = (_V3-_V2)*height;
  5929.     LeftDi    = (_I3-_I1)*height;  // compute intensity deltas.
  5930.     RightDi   = (_I3-_I2)*height; 
  5931.     LeftDh    = (h3-h1)*height;
  5932.     RightDh   = (h3-h2)*height;
  5933.     
  5934.     LeftX     = _X1<<16;
  5935.     RightX    = (_X2<<16)+32768;
  5936.     LeftU     = _U1<<16;
  5937.     LeftV     = _V1<<16;
  5938.     RightU    = _U2<<16;
  5939.     RightV    = _V2<<16;
  5940.     LeftI     = _I1<<16;              // assign intensity to left and right side.
  5941.     RightI    = _I2<<16;
  5942.     LeftH     = h1<<16;
  5943.     RightH    = h2<<16;
  5944.  
  5945.     ClipLeftX  = ( LeftX>>16 );
  5946.     ClipRightX = ( RightX>>16 );
  5947.     width      = 1+ClipRightX-ClipLeftX;
  5948.      
  5949.     ScanU    = (RightU-LeftU)/width;
  5950.     ScanV    = (RightV-LeftV)/width;            
  5951.     MiddleDi = (RightI-LeftI)/width;          
  5952.     MiddleDh = (RightH-LeftH)/width;
  5953.                             
  5954.     if (_Y1 < _MinClipY)
  5955.     {
  5956.        ydiff    = (_MinClipY - _Y1);
  5957.        LeftX    = LeftX+LeftDx*ydiff;
  5958.        RightX   = RightX+RightDx*ydiff;
  5959.  
  5960.        LeftU    = LeftU+LeftDu*ydiff;
  5961.        RightU   = RightU+RightDu*ydiff;
  5962.        LeftV    = LeftV+LeftDv*ydiff;
  5963.        RightV   = RightV+RightDv*ydiff;
  5964.  
  5965.        LeftI    = LeftI+LeftDi*ydiff;
  5966.        RightI   = RightI+RightDi*ydiff;
  5967.  
  5968.        LeftH    = LeftH+LeftDh*ydiff;
  5969.        RightH   = RightH+RightDh*ydiff;
  5970.        
  5971.        ClipLeftX  = ( LeftX>>16 );
  5972.        ClipRightX = ( RightX>>16 );
  5973.        width      = 1+ClipRightX-ClipLeftX;
  5974.      
  5975.        ScanU    = (RightU-LeftU)/width;
  5976.        ScanV    = (RightV-LeftV)/width;            
  5977.        MiddleDi = (RightI-LeftI)/width;          
  5978.        MiddleDh = (RightH-LeftH)/width;
  5979.        
  5980.              _Y1 = _MinClipY;
  5981.     }
  5982.                 
  5983.     if (_Y3 > _MaxClipY)
  5984.       _Y3 = _MaxClipY;
  5985.               
  5986.     Buffer+=(_Y1<<8)+(_Y1<<6);
  5987.  
  5988.     if (_X1>=_MinClipX && _X1<=_MaxClipX &&
  5989.         _X2>=_MinClipX && _X2<=_MaxClipX &&
  5990.         _X3>=_MinClipX && _X3<=_MaxClipX)
  5991.     {                   
  5992.        for (y=_Y1;y<_Y3;y++,Buffer+=SCREENWIDTH)    
  5993.        {        
  5994.           u=LeftU;
  5995.           v=LeftV;
  5996.           MiddleI=LeftI;
  5997.           MiddleH=LeftH;
  5998.           
  5999.           for (x=( ClipLeftX );x<=( ClipRightX );x++)
  6000.           {                 
  6001.              Buffer[x]=HazeTBL[ (LookPal[ TextureMap[ (u>>16)+( (v>>16)<<8) ] + (MiddleI>>16) ]<<6) + (MiddleH>>16) ];
  6002.              u+=ScanU;
  6003.              v+=ScanV;
  6004.              MiddleI+=MiddleDi;
  6005.              MiddleH+=MiddleDh;
  6006.           }       
  6007.           LeftX   += LeftDx;
  6008.           RightX  += RightDx;
  6009.           LeftU   += LeftDu;
  6010.           LeftV   += LeftDv;
  6011.           RightU  += RightDu;
  6012.           RightV  += RightDv;
  6013.           LeftI   += LeftDi;      // update the intensities and slopes.
  6014.           RightI  += RightDi;
  6015.           LeftH   += LeftDh;
  6016.           RightH  += RightDh;
  6017.  
  6018.           ClipLeftX  = ( LeftX>>16 );
  6019.           ClipRightX = ( RightX>>16 );
  6020.           width      = 1+ClipRightX-ClipLeftX;
  6021.      
  6022.           ScanU    = (RightU-LeftU)/width;
  6023.           ScanV    = (RightV-LeftV)/width;            
  6024.           MiddleDi = (RightI-LeftI)/width;          
  6025.           MiddleDh = (RightH-LeftH)/width;
  6026.        }
  6027.     }
  6028.     else
  6029.     {
  6030.        for (y=_Y1;y<_Y3;y++,Buffer+=SCREENWIDTH)
  6031.        {
  6032.  
  6033.           u=LeftU;
  6034.           v=LeftV;
  6035.           MiddleI=LeftI;
  6036.           MiddleH=LeftH;
  6037.           
  6038.           if (ClipLeftX < _MinClipX)
  6039.           {
  6040.             if (ClipRightX <= _MinClipX)
  6041.             {
  6042.                LeftX  += LeftDx;      // update the intensities and slopes.
  6043.                RightX += RightDx;     // before continuing.
  6044.                LeftU  += LeftDu;
  6045.                LeftV  += LeftDv;
  6046.                RightU += RightDu;
  6047.                RightV += RightDv;
  6048.                LeftI  += LeftDi;      
  6049.                RightI += RightDi;         
  6050.                LeftH  += LeftDh;
  6051.                RightH += RightDh;              
  6052.  
  6053.                ClipLeftX  = ( LeftX>>16 );
  6054.                ClipRightX = ( RightX>>16 );
  6055.  
  6056.                continue;
  6057.             }
  6058.             u          = LeftU + (_MinClipX - ClipLeftX)*ScanU;
  6059.             v          = LeftV + (_MinClipX - ClipLeftX)*ScanV;
  6060.             MiddleI    = LeftI + (_MinClipX - ClipLeftX)*MiddleDi;   // move the intensity by
  6061.             MiddleH    = LeftH + (_MinClipX - ClipLeftX)*MiddleDh;
  6062.             ClipLeftX  = _MinClipX;   // the clipped amount.
  6063.           }
  6064.  
  6065.           if (ClipRightX > _MaxClipX)
  6066.           {
  6067.             if (ClipLeftX > _MaxClipX)
  6068.             {
  6069.                LeftX  += LeftDx;      // update the intensities and slopes.
  6070.                RightX += RightDx;     // before continuing.
  6071.                LeftU  += LeftDu;
  6072.                LeftV  += LeftDv;
  6073.                RightU += RightDu;
  6074.                RightV += RightDv;
  6075.                LeftI  += LeftDi;      
  6076.                RightI += RightDi;
  6077.                LeftH  += LeftDh;
  6078.                RightH += RightDh;              
  6079.  
  6080.                ClipLeftX  = ( LeftX>>16 );
  6081.                ClipRightX = ( RightX>>16 );
  6082.  
  6083.                continue;
  6084.             }
  6085.             ClipRightX = _MaxClipX;
  6086.           }
  6087.                                             
  6088.           for (x=ClipLeftX;x<ClipRightX;x++)
  6089.           {
  6090.              Buffer[x]=HazeTBL[ (LookPal[ TextureMap[ (u>>16)+( (v>>16)<<8) ] + (MiddleI>>16) ]<<6) + (MiddleH>>16) ];
  6091.              u+=ScanU;
  6092.              v+=ScanV;
  6093.              MiddleI+=MiddleDi;
  6094.              MiddleH+=MiddleDh;
  6095.           }               
  6096.           LeftX   += LeftDx;
  6097.           RightX  += RightDx;
  6098.           LeftU   += LeftDu;
  6099.           LeftV   += LeftDv;
  6100.           RightU  += RightDu;
  6101.           RightV  += RightDv;
  6102.           LeftI   += LeftDi;     
  6103.           RightI  += RightDi;         
  6104.           LeftH   += LeftDh;
  6105.           RightH  += RightDh;          
  6106.  
  6107.           ClipLeftX  = ( LeftX>>16 );
  6108.           ClipRightX = ( RightX>>16 );
  6109.           width      = 1+ClipRightX-ClipLeftX;
  6110.      
  6111.           ScanU    = (RightU-LeftU)/width;
  6112.           ScanV    = (RightV-LeftV)/width;            
  6113.           MiddleDi = (RightI-LeftI)/width;          
  6114.           MiddleDh = (RightH-LeftH)/width;
  6115.        }
  6116.     }
  6117. }
  6118.  
  6119. void Scan_Convert_TexturePH(void)
  6120. {
  6121.   long LeftX, RightX, LeftDx, RightDx;
  6122.   long u,v,ScanU,ScanV,LeftU,LeftV,RightU,RightV,LeftA,RightA,MiddleA;
  6123.   long LeftDu,LeftDv,RightDu,RightDv,LeftDa,RightDa,MiddleDa;
  6124.   long hazevalue,height,width,slope,ydiff;
  6125.   long LeftH,RightH,MiddleH;
  6126.   long LeftDh,RightDh,MiddleDh;
  6127.   int h1,h2,h3,newh,ClipLeftX,ClipRightX;
  6128.   int x,y,newa,newx,newu,newv,tempx,tempy,tempu,tempv,tempa,temph;
  6129.   int old_X3,old_Y3,old_U3,old_V3,old_A3,oldh3;
  6130.   int GENERAL;
  6131.   unsigned char *Buffer;
  6132.   
  6133.     if ( (_X1==_X2 && _X2==_X3) || (_Y1==_Y2 && _Y2==_Y3) )
  6134.       return;               // degenerated into lines.       
  6135.  
  6136.     if (_Y2<_Y1)              // switch. They're in the wrong order.
  6137.     {
  6138.         tempx=_X1; tempy=_Y1;
  6139.         _X1=_X2;    _Y1=_Y2;
  6140.         _X2=tempx; _Y2=tempy;
  6141.  
  6142.         tempu=_U1; tempv=_V1; tempa=_A1;
  6143.         _U1=_U2;    _V1=_V2;    _A1=_A2;
  6144.         _U2=tempu; _V2=tempv; _A2=tempa;
  6145.     }    
  6146.     if (_Y3<_Y1)              // switch. They're in the wrong order.
  6147.     {
  6148.         tempx=_X1; tempy=_Y1;
  6149.         _X1=_X3;    _Y1=_Y3;
  6150.         _X3=tempx; _Y3=tempy;
  6151.  
  6152.         tempu=_U1; tempv=_V1; tempa=_A1;
  6153.         _U1=_U3;    _V1=_V3;    _A1=_A3;
  6154.         _U3=tempu; _V3=tempv; _A3=tempa;
  6155.     }
  6156.     if (_Y3<_Y2)              // switch. They're in the wrong order.
  6157.     {
  6158.         tempx=_X2; tempy=_Y2;
  6159.         _X2=_X3;    _Y2=_Y3;
  6160.         _X3=tempx; _Y3=tempy;
  6161.  
  6162.         tempu=_U2; tempv=_V2; tempa=_A2;
  6163.         _U2=_U3;    _V2=_V3;    _A2=_A3;
  6164.         _U3=tempu; _V3=tempv; _A3=tempa;
  6165.     }    
  6166.  
  6167.     if (_Y3<_MinClipY || _Y1>_MaxClipY ||
  6168.        (_X1<_MinClipX && _X2<_MinClipX && _X3<_MinClipX) ||
  6169.        (_X1>_MaxClipX && _X2>_MaxClipX && _X3>_MaxClipX) )
  6170.        return;              // do trivial rejection.
  6171.         
  6172.     GENERAL=FALSE;          // reset cases (for Triangle).
  6173.     Buffer=_RendBuffer;     // save starting point of Buffer.
  6174.     
  6175.     hazevalue=(_Z1<<16)/1000;
  6176.     h1=(64*hazevalue)>>16;
  6177.     hazevalue=(_Z2<<16)/1000;
  6178.     h2=(64*hazevalue)>>16;
  6179.     hazevalue=(_Z3<<16)/1000;
  6180.     h3=(64*hazevalue)>>16;
  6181.  
  6182.     if (_Y1==_Y2)
  6183.       goto FLAT_TOP;
  6184.     if (_Y2==_Y3)
  6185.       goto FLAT_BOTTOM;
  6186.     else
  6187.       GENERAL=TRUE;
  6188.          
  6189.     height  = 65536/(_Y3-_Y1);
  6190.     slope   = (_X3-_X1)*height;               
  6191.     newx    = _X1+( (slope*(_Y2-_Y1))>>16 );
  6192.     newu    = (((_Y2-_Y1)*_U3+(_Y3-_Y2)*_U1)*height)>>16;
  6193.     newv    = (((_Y2-_Y1)*_V3+(_Y3-_Y2)*_V1)*height)>>16;
  6194.     newa    = (((_Y2-_Y1)*_A3+(_Y3-_Y2)*_A1)*height)>>16;
  6195.     newh    = (((_Y2-_Y1)*h3+(_Y3-_Y2)*h1)*height)>>16;
  6196.  
  6197.     old_X3    = _X3;                            // save values for later.
  6198.     old_Y3    = _Y3;
  6199.     old_U3    = _U3;
  6200.     old_V3    = _V3;
  6201.     old_A3    = _A3;       
  6202.     oldh3    = h3;    
  6203.     
  6204.     _X3      = newx;
  6205.     _Y3      = _Y2;           
  6206.     _U3      = newu;
  6207.     _V3      = newv;
  6208.     _A3      = newa;
  6209.     h3      = newh;
  6210.     
  6211.     FLAT_BOTTOM:
  6212.  
  6213.     if (_X3<_X2)
  6214.     {
  6215.       tempx=_X3; _X3=_X2; _X2=tempx;
  6216.       tempu=_U3; _U3=_U2; _U2=tempu;
  6217.       tempv=_V3; _V3=_V2; _V2=tempv;
  6218.       tempa=_A3; _A3=_A2; _A2=tempa;
  6219.       temph=h3; h3=h2; h2=temph;
  6220.     }
  6221.  
  6222.     height  = 65536/(_Y3-_Y1);
  6223.     LeftDx  = (_X2-_X1)*height;    // Inverse left and right slope.
  6224.     RightDx = (_X3-_X1)*height;
  6225.     LeftDu  = (_U2-_U1)*height;
  6226.     LeftDv  = (_V2-_V1)*height;
  6227.     RightDu = (_U3-_U1)*height;
  6228.     RightDv = (_V3-_V1)*height;
  6229.     LeftDa  = (_A2-_A1)*height;  // compute intensity deltas.
  6230.     RightDa = (_A3-_A1)*height; 
  6231.     LeftDh  = (h2-h1)*height;
  6232.     RightDh = (h3-h1)*height;
  6233.     
  6234.     LeftX   = _X1<<16;
  6235.     RightX  = LeftX+32768;
  6236.     LeftU   = _U1<<16;
  6237.     LeftV   = _V1<<16;
  6238.     RightU  = LeftU;
  6239.     RightV  = LeftV;
  6240.     LeftA   = _A1<<16;              // assign intensity to left and right side.
  6241.     RightA  = LeftA;
  6242.     LeftH   = h1<<16;
  6243.     RightH  = LeftH;
  6244.     
  6245.     ScanU    = 0;              // because of Flat bottom.
  6246.     ScanV    = 0;
  6247.     MiddleDa = 0;
  6248.     MiddleDh = 0;
  6249.     
  6250.     ClipLeftX  = ( LeftX>>16 );
  6251.     ClipRightX = ( RightX>>16 );
  6252.           
  6253.     if (_Y1 < _MinClipY)
  6254.     {
  6255.        ydiff    = (_MinClipY - _Y1);
  6256.        LeftX    = LeftX+LeftDx*ydiff;
  6257.        RightX   = RightX+RightDx*ydiff;
  6258.  
  6259.        LeftU    = LeftU+LeftDu*ydiff;
  6260.        RightU   = RightU+RightDu*ydiff;
  6261.        LeftV    = LeftV+LeftDv*ydiff;
  6262.        RightV   = RightV+RightDv*ydiff;
  6263.  
  6264.        LeftA    = LeftA+LeftDa*ydiff;
  6265.        RightA   = RightA+RightDa*ydiff;
  6266.  
  6267.        LeftH    = LeftH+LeftDh*ydiff;
  6268.        RightH   = RightH+RightDh*ydiff;
  6269.  
  6270.        ClipLeftX  = ( LeftX>>16 );
  6271.        ClipRightX = ( RightX>>16 );       
  6272.        width      = 1+ClipRightX-ClipLeftX;
  6273.  
  6274.        ScanU    = (RightU-LeftU)/width;
  6275.        ScanV    = (RightV-LeftV)/width;
  6276.        MiddleDa = (RightA-LeftA)/width;          
  6277.        MiddleDh = (RightH-LeftH)/width;
  6278.  
  6279.            _Y1   = _MinClipY;
  6280.     }
  6281.                 
  6282.     if (_Y3 > _MaxClipY)
  6283.       _Y3 = _MaxClipY;
  6284.               
  6285.     Buffer+=(_Y1<<8)+(_Y1<<6);
  6286.  
  6287.     if (_X1>=_MinClipX && _X1<=_MaxClipX &&
  6288.         _X2>=_MinClipX && _X2<=_MaxClipX &&
  6289.         _X3>=_MinClipX && _X3<=_MaxClipX)
  6290.     {                   
  6291.        for (y=_Y1;y<_Y3;y++,Buffer+=SCREENWIDTH)    
  6292.        {
  6293.           u=LeftU;
  6294.           v=LeftV;
  6295.           MiddleA=LeftA;
  6296.           MiddleH=LeftH;
  6297.           for (x=( ClipLeftX );x<=( ClipRightX );x++)
  6298.           {                 
  6299.              Buffer[x]=HazeTBL[ (LookPalPhong[ TextureMap[ (u>>16)+( (v>>16)<<8) ] + PhongTBL[ (MiddleA>>16) ] ]<<6) + (MiddleH>>16) ];
  6300.              u+=ScanU;
  6301.              v+=ScanV;
  6302.              MiddleA+=MiddleDa;
  6303.              MiddleH+=MiddleDh;
  6304.           }       
  6305.           LeftX   += LeftDx;
  6306.           RightX  += RightDx;
  6307.           LeftU   += LeftDu;
  6308.           LeftV   += LeftDv;
  6309.           RightU  += RightDu;
  6310.           RightV  += RightDv;
  6311.           LeftA   += LeftDa;      // update the intensities and slopes.
  6312.           RightA  += RightDa;
  6313.           LeftH   += LeftDh;
  6314.           RightH  += RightDh;
  6315.  
  6316.           ClipLeftX  = ( LeftX>>16 );
  6317.           ClipRightX = ( RightX>>16 );
  6318.           width      = 1+ClipRightX-ClipLeftX;
  6319.  
  6320.           ScanU    = (RightU-LeftU)/width;
  6321.           ScanV    = (RightV-LeftV)/width;            
  6322.           MiddleDa = (RightA-LeftA)/width;          
  6323.           MiddleDh = (RightH-LeftH)/width;
  6324.        }
  6325.     }
  6326.     else
  6327.     {
  6328.        for (y=_Y1;y<_Y3;y++,Buffer+=SCREENWIDTH)    
  6329.        {
  6330.  
  6331.           u=LeftU;
  6332.           v=LeftV;
  6333.           MiddleA=LeftA;                                            
  6334.           MiddleH=LeftH;
  6335.           
  6336.           if (ClipLeftX < _MinClipX)
  6337.           {
  6338.             if (ClipRightX <= _MinClipX)
  6339.             {
  6340.                LeftX  += LeftDx;      // update the intensities and slopes.
  6341.                RightX += RightDx;     // before continuing.
  6342.                LeftU  += LeftDu;
  6343.                LeftV  += LeftDv;
  6344.                RightU += RightDu;
  6345.                RightV += RightDv;
  6346.                LeftA  += LeftDa;      
  6347.                RightA += RightDa;
  6348.                LeftH  += LeftDh;
  6349.                RightH += RightDh;              
  6350.  
  6351.                ClipLeftX  = ( LeftX>>16 );
  6352.                ClipRightX = ( RightX>>16 );
  6353.  
  6354.                continue;
  6355.             }
  6356.             u         = LeftU + (_MinClipX - ClipLeftX)*ScanU;
  6357.             v         = LeftV + (_MinClipX - ClipLeftX)*ScanV;
  6358.             MiddleA   = LeftA + (_MinClipX - ClipLeftX)*MiddleDa;   // move the intensity by  
  6359.             MiddleH   = LeftH + (_MinClipX - ClipLeftX)*MiddleDh;
  6360.             ClipLeftX = _MinClipX;     // the clipped amount.
  6361.           }
  6362.  
  6363.           if (ClipRightX > _MaxClipX)
  6364.           {
  6365.             if (ClipLeftX > _MaxClipX)
  6366.             {
  6367.                LeftX  += LeftDx;      // update the intensities and slopes.
  6368.                RightX += RightDx;     // before continuing.
  6369.                LeftU  += LeftDu;
  6370.                LeftV  += LeftDv;
  6371.                RightU += RightDu;
  6372.                RightV += RightDv;
  6373.                LeftA  += LeftDa;      
  6374.                RightA += RightDa;
  6375.                LeftH  += LeftDh;
  6376.                RightH += RightDh;       
  6377.  
  6378.                ClipLeftX  = ( LeftX>>16 );
  6379.                ClipRightX = ( RightX>>16 );
  6380.  
  6381.                continue;
  6382.             }
  6383.             ClipRightX = _MaxClipX;
  6384.           }
  6385.           
  6386.           for (x=ClipLeftX;x<ClipRightX;x++)
  6387.           {
  6388.              Buffer[x]=HazeTBL[ (LookPalPhong[ TextureMap[ (u>>16)+( (v>>16)<<8) ] + PhongTBL[ (MiddleA>>16) ] ]<<6) + (MiddleH>>16) ];
  6389.              u+=ScanU;
  6390.              v+=ScanV;
  6391.              MiddleA+=MiddleDa;
  6392.              MiddleH+=MiddleDh;
  6393.           }               
  6394.         LeftX   += LeftDx;
  6395.         RightX  += RightDx;
  6396.         LeftU   += LeftDu;
  6397.         LeftV   += LeftDv;
  6398.         RightU  += RightDu;
  6399.         RightV  += RightDv;
  6400.         LeftA   += LeftDa;      // update the intensities and slopes.
  6401.         RightA  += RightDa;
  6402.         LeftH   += LeftDh;
  6403.         RightH  += RightDh;
  6404.  
  6405.         ClipLeftX  = ( LeftX>>16 );
  6406.         ClipRightX = ( RightX>>16 );
  6407.         width      = 1+ClipRightX-ClipLeftX;
  6408.  
  6409.         ScanU    = (RightU-LeftU)/width;
  6410.         ScanV    = (RightV-LeftV)/width;            
  6411.         MiddleDa = (RightA-LeftA)/width;          
  6412.         MiddleDh = (RightH-LeftH)/width;
  6413.        }
  6414.     }
  6415.  
  6416.     if (!GENERAL)
  6417.       return;
  6418.  
  6419.     _X1 = _X2;                 // setup for FLAT_TOP.
  6420.     _Y1 = _Y2;
  6421.     _U1 = _U2;
  6422.     _V1 = _V2;
  6423.     _A1 = _A2;
  6424.     h1 = h2;
  6425.     
  6426.     _X2 = _X3;
  6427.     _U2 = _U3;
  6428.     _V2 = _V3;
  6429.     _A2 = _A3;
  6430.     h2 = h3;
  6431.     
  6432.     _X3 = old_X3;
  6433.     _Y3 = old_Y3;
  6434.     _U3 = old_U3;
  6435.     _V3 = old_V3;
  6436.     _A3 = old_A3;
  6437.     h3 = oldh3;
  6438.     
  6439.     Buffer=_RendBuffer;     // save starting point of Buffer.
  6440.     
  6441.     FLAT_TOP: 
  6442.  
  6443.     if (_X2<_X1)
  6444.     {
  6445.       tempx=_X2; _X2=_X1; _X1=tempx;
  6446.       tempu=_U2; _U2=_U1; _U1=tempu;
  6447.       tempv=_V2; _V2=_V1; _V1=tempv;
  6448.       tempa=_A2; _A2=_A1; _A1=tempa;
  6449.       temph=h2; h2=h1; h1=temph;
  6450.     }
  6451.  
  6452.     height    = 65536/(_Y3-_Y1);
  6453.  
  6454.     LeftDx    = (_X3-_X1)*height;    // Inverse left and right slope.
  6455.     RightDx   = (_X3-_X2)*height;
  6456.     LeftDu    = (_U3-_U1)*height;
  6457.     LeftDv    = (_V3-_V1)*height;
  6458.     RightDu   = (_U3-_U2)*height;
  6459.     RightDv   = (_V3-_V2)*height;
  6460.     LeftDa    = (_A3-_A1)*height;  // compute intensity deltas.
  6461.     RightDa   = (_A3-_A2)*height; 
  6462.     LeftDh    = (h3-h1)*height;
  6463.     RightDh   = (h3-h2)*height;
  6464.     
  6465.     LeftX     = _X1<<16;
  6466.     RightX    = (_X2<<16)+32768;
  6467.     LeftU     = _U1<<16;
  6468.     LeftV     = _V1<<16;
  6469.     RightU    = _U2<<16;
  6470.     RightV    = _V2<<16;
  6471.     LeftA     = _A1<<16;              // assign intensity to left and right side.
  6472.     RightA    = _A2<<16;
  6473.     LeftH     = h1<<16;
  6474.     RightH    = h2<<16;
  6475.  
  6476.     ClipLeftX  = ( LeftX>>16 );
  6477.     ClipRightX = ( RightX>>16 );
  6478.     width      = 1+ClipRightX-ClipLeftX;
  6479.      
  6480.      ScanU    = (RightU-LeftU)/width;
  6481.      ScanV    = (RightV-LeftV)/width;            
  6482.      MiddleDa = (RightA-LeftA)/width;          
  6483.      MiddleDh = (RightH-LeftH)/width;
  6484.                             
  6485.     if (_Y1 < _MinClipY)
  6486.     {
  6487.        ydiff    = (_MinClipY - _Y1);
  6488.        LeftX    = LeftX+LeftDx*ydiff;
  6489.        RightX   = RightX+RightDx*ydiff;
  6490.  
  6491.        LeftU    = LeftU+LeftDu*ydiff;
  6492.        RightU   = RightU+RightDu*ydiff;
  6493.        LeftV    = LeftV+LeftDv*ydiff;
  6494.        RightV   = RightV+RightDv*ydiff;
  6495.  
  6496.        LeftA    = LeftA+LeftDa*ydiff;
  6497.        RightA   = RightA+RightDa*ydiff;
  6498.  
  6499.        LeftH    = LeftH+LeftDh*ydiff;
  6500.        RightH   = RightH+RightDh*ydiff;
  6501.        
  6502.        ClipLeftX  = ( LeftX>>16 );
  6503.        ClipRightX = ( RightX>>16 );
  6504.        width      = 1+ClipRightX-ClipLeftX;
  6505.      
  6506.        ScanU    = (RightU-LeftU)/width;
  6507.        ScanV    = (RightV-LeftV)/width;            
  6508.        MiddleDa = (RightA-LeftA)/width;          
  6509.        MiddleDh = (RightH-LeftH)/width;
  6510.        
  6511.              _Y1 = _MinClipY;
  6512.     }
  6513.                 
  6514.     if (_Y3 > _MaxClipY)
  6515.       _Y3 = _MaxClipY;
  6516.               
  6517.     Buffer+=(_Y1<<8)+(_Y1<<6);
  6518.  
  6519.     if (_X1>=_MinClipX && _X1<=_MaxClipX &&
  6520.         _X2>=_MinClipX && _X2<=_MaxClipX &&
  6521.         _X3>=_MinClipX && _X3<=_MaxClipX)
  6522.     {                   
  6523.        for (y=_Y1;y<_Y3;y++,Buffer+=SCREENWIDTH)    
  6524.        {        
  6525.           u=LeftU;
  6526.           v=LeftV;
  6527.           MiddleA=LeftA;
  6528.           MiddleH=LeftH;
  6529.           
  6530.           for (x=( ClipLeftX );x<=( ClipRightX );x++)
  6531.           {                 
  6532.              Buffer[x]=HazeTBL[ (LookPalPhong[ TextureMap[ (u>>16)+( (v>>16)<<8) ] + PhongTBL[ (MiddleA>>16) ] ]<<6) + (MiddleH>>16) ];
  6533.              u+=ScanU;
  6534.              v+=ScanV;
  6535.              MiddleA+=MiddleDa;
  6536.              MiddleH+=MiddleDh;
  6537.           }       
  6538.           LeftX   += LeftDx;
  6539.           RightX  += RightDx;
  6540.           LeftU   += LeftDu;
  6541.           LeftV   += LeftDv;
  6542.           RightU  += RightDu;
  6543.           RightV  += RightDv;
  6544.           LeftA   += LeftDa;      // update the intensities and slopes.
  6545.           RightA  += RightDa;
  6546.           LeftH   += LeftDh;
  6547.           RightH  += RightDh;
  6548.  
  6549.           ClipLeftX  = ( LeftX>>16 );
  6550.           ClipRightX = ( RightX>>16 );
  6551.           width      = 1+ClipRightX-ClipLeftX;
  6552.      
  6553.           ScanU    = (RightU-LeftU)/width;
  6554.           ScanV    = (RightV-LeftV)/width;            
  6555.           MiddleDa = (RightA-LeftA)/width;          
  6556.           MiddleDh = (RightH-LeftH)/width;
  6557.        }
  6558.     }
  6559.     else
  6560.     {
  6561.        for (y=_Y1;y<_Y3;y++,Buffer+=SCREENWIDTH)
  6562.        {
  6563.  
  6564.           u=LeftU;
  6565.           v=LeftV;
  6566.           MiddleA=LeftA;
  6567.           MiddleH=LeftH;
  6568.           
  6569.           if (ClipLeftX < _MinClipX)
  6570.           {
  6571.             if (ClipRightX <= _MinClipX)
  6572.             {
  6573.                LeftX  += LeftDx;      // update the intensities and slopes.
  6574.                RightX += RightDx;     // before continuing.
  6575.                LeftU  += LeftDu;
  6576.                LeftV  += LeftDv;
  6577.                RightU += RightDu;
  6578.                RightV += RightDv;
  6579.                LeftA  += LeftDa;      
  6580.                RightA += RightDa;         
  6581.                LeftH  += LeftDh;
  6582.                RightH += RightDh;              
  6583.  
  6584.                ClipLeftX  = ( LeftX>>16 );
  6585.                ClipRightX = ( RightX>>16 );
  6586.  
  6587.                continue;
  6588.             }
  6589.             u          = LeftU + (_MinClipX - ClipLeftX)*ScanU;
  6590.             v          = LeftV + (_MinClipX - ClipLeftX)*ScanV;
  6591.             MiddleA    = LeftA + (_MinClipX - ClipLeftX)*MiddleDa;   // move the intensity by
  6592.             MiddleH    = LeftH + (_MinClipX - ClipLeftX)*MiddleDh;
  6593.             ClipLeftX  = _MinClipX;   // the clipped amount.
  6594.           }
  6595.  
  6596.           if (ClipRightX > _MaxClipX)
  6597.           {
  6598.             if (ClipLeftX > _MaxClipX)
  6599.             {
  6600.                LeftX  += LeftDx;      // update the intensities and slopes.
  6601.                RightX += RightDx;     // before continuing.
  6602.                LeftU  += LeftDu;
  6603.                LeftV  += LeftDv;
  6604.                RightU += RightDu;
  6605.                RightV += RightDv;
  6606.                LeftA  += LeftDa;      
  6607.                RightA += RightDa;
  6608.                LeftH  += LeftDh;
  6609.                RightH += RightDh;              
  6610.  
  6611.                ClipLeftX  = ( LeftX>>16 );
  6612.                ClipRightX = ( RightX>>16 );
  6613.  
  6614.                continue;
  6615.             }
  6616.             ClipRightX = _MaxClipX;
  6617.           }
  6618.                                             
  6619.           for (x=ClipLeftX;x<ClipRightX;x++)
  6620.           {
  6621.              Buffer[x]=HazeTBL[ (LookPalPhong[ TextureMap[ (u>>16)+( (v>>16)<<8) ] + PhongTBL[ (MiddleA>>16) ] ]<<6) + (MiddleH>>16) ];
  6622.              u+=ScanU;
  6623.              v+=ScanV;
  6624.              MiddleA+=MiddleDa;
  6625.              MiddleH+=MiddleDh;
  6626.           }
  6627.           LeftX   += LeftDx;
  6628.           RightX  += RightDx;
  6629.           LeftU   += LeftDu;
  6630.           LeftV   += LeftDv;
  6631.           RightU  += RightDu;
  6632.           RightV  += RightDv;
  6633.           LeftA   += LeftDa;     
  6634.           RightA  += RightDa;         
  6635.           LeftH   += LeftDh;
  6636.           RightH  += RightDh;          
  6637.  
  6638.           ClipLeftX  = ( LeftX>>16 );
  6639.           ClipRightX = ( RightX>>16 );
  6640.           width      = 1+ClipRightX-ClipLeftX;
  6641.      
  6642.           ScanU    = (RightU-LeftU)/width;
  6643.           ScanV    = (RightV-LeftV)/width;            
  6644.           MiddleDa = (RightA-LeftA)/width;          
  6645.           MiddleDh = (RightH-LeftH)/width;
  6646.        }
  6647.     }
  6648. }
  6649.